Friday, July 20, 2012

The importance of .htaccess | Using .htaccess Rewrite Rules | .htaccess Example


The importance of .htaccess | Using .htaccess Rewrite Rules | .htaccess Example
.htaccess Rewrite Rules
Hypertext Access, commonly abbreviated to .htaccess is a configuration file for use on web servers like Apache Server. It is used to control the directory it is placed in and all the subdirectories under it. This file indicates which users of groups of users can be allowed to access to the files which are placed in that directory. Htaccess file has the capability to restrict access to some folders or subfolders on the internet through its password protection. This file also enables to block or allow certain IP address access. It commands over the server and guide the server as how to react and behave in certain situations. This file is created in notepad or text editor, after creation it is placed in the Root Directory of the Web Server.
Through .htaccess we can:
  1. Password Protection: If we want a specific area or folder to be protected and want only certain people can access it then with .htaccess file you are safely protected.
  2. 301 Redirect: By redirection we refer to direct visitor from one web page to another within the websiteIt is considered the most effective and search friendly way to redirect pages, URLs, etc. If we have moved our webpage content then with .htaccess file we can redirect users from old links to new webpage content links.
  3. Block Users: We can block various bots with .htaccess file like we can keep spammers, users not to access the certain area of the website. This is used to avoid unwanted visitors to access the website area where only specific people can have access to the website.
  4. Custom 404 Pages: Through customized error page creation we can make visitor provided with the search friendly message instead of “not found” error message. Here we can define error pages to maintain communication with the visitor.
Properties of .htaccess
  • An .htaccess file is a hidden file like any other beginning with a dot. They are called dot files.
  • It must be a plain text file created in notepad or text editor.
  • An .htaccess applies server directories to a directory and all subdirectories.
  • An .htaccess is not the name of the file, it is a file extension. It is simply called .htaccess.
Example – 301 Redirects in Apache .htaccess
Redirect WWW to non-WWW:
RewriteEngine On
RewriteCond %{ HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www. example.com/$1 [R=301,L]
Example – 301 Redirects in IIS .htaccess
With IIS7, there are actually two ways to do this. The URL Rewrite extension is required for this.
1. Redirect WWW to non-WWW:
<rewrite>
<rules>
<rule name=”www to non www”" enabled=”true”>
url=”(.*)” />
<conditions>
<add input=”{HTTP_HOST}” negate=”true” pattern=”^www\.example\.com$” />
</conditions>
<action type=”Redirect” url=http://www\.example\.com/{R:1}” redirectType=”Permanent” />
</rule>
</rules>
</rewrite>
2. Redirect non-WWW to WWW:
<rewrite>
<rules>
<rule name=”non www to www” enabled=”true”>
url=”(.*)” />
<conditions>
<add input=”{HTTP_HOST}” negate=”true” pattern=”^www\.example\.com$” />
</conditions>
<action type=”Redirect” url=”http://www\.example.\com/{R:0}” redirectType=”Permanent” />
</rule>
</rules>
</rewrite>
Quick, easy-to-understand tips for .Htaccess
Symbols
Why used
^example\.com$
It is the Regular expression  to which we compare the referrer
“^”
It means string start
“\”
used to escape the dots (which otherwise are part of Regular expression syntax
“$”
Means string end.
[L]
flag causes mod_rewrite to stop processing the rule set
  • [NC] means we don’t care about the case.
  • RewriteCond %{ HTTP_HOST} ^example\.com$ [NC]: RewriteCond is a directive which defines a condition under which rewriting will take place.
  • The syntax is RewriteCond $StringToTest $Regular expression [$flags].
  • RewriteRule is a directive which defines rules for the rewriting engine.
  • The syntax is RewriteRule $ Regular expression $Substitution [$flags].

No comments:

Post a Comment