Monday, February 7, 2011

What can I do with a .htaccess file?

The .htaccess file allows you to override some settings of the web server which will allow you to control the way your web site responds to visitors.  We support a number of directives and configurations which you may use to change the behavior of your web site. Below are some examples.

Error Documents

Please refer to the following document which will describe in detail how to use a .htaccess file to enable error document handling.

Rewrite Engine (mod_rewrite)

The rewrite engine, or mod_rewrite allows you to alter the URL displayed in the address bar while redirecting the visitor to a different page of your choice.  While it is a very powerful feature and can accomplish a lot of tricks, it is usually used to create search engine friendly URLs.

Example 1: mod_rewrite

Address bar shows:
Actual web page served:
1RewriteEngine On
2RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2

Example 2: mod_rewrite

Address bar shows:
Actual web page served:
1RewriteEngine on
2RewriteBase /directory
3RewriteRule ^([a-z]+).html$ /index.php?p=$1 [R,L]

Example 3: Force index.php to handle all requests

For dynamic applications which use a single file to control the web site, it is often desirable to force all URLs to be passed to a single script or file.  This will allow you to create very flexible search engine friendly URLs and let your application’s code decide which page to display.  This code example comes from Wordpress and shows how every URL is passed to index.php which then determines which page to display.
view sourceprint?
1RewriteEngine On
2RewriteBase /
3RewriteCond %{REQUEST_FILENAME} !-f
4RewriteCond %{REQUEST_FILENAME} !-d
5RewriteRule . /index.php [L]