Monday, February 25, 2008


.htaccess is an access control file that provides a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof. .htaccess files should be used in a case where the content providers need to make configuration changes to the server on a per-directory basis, but do not have root access on the server system. In the event that the server administrator is not willing to make frequent configuration changes, it might be desirable to permit individual users to make these changes in .htaccess files for themselves.

Note: If you want to call your .htaccess file something else, you can change the name of the file using the AccessFileName directive. For example, if you would rather call the file .config then you can put the following in your server configuration file:

AccessFileName .config

Some tips about .htaccess file
CHMOD the htaccess file to 644 or (RW-R--R--)

htaccess files must be uploaded as ASCII mode, not BINARY

htaccess files affect the directory they are placed in and all sub-directories.

The use of .htaccess files can be disabled completely by setting the AllowOverride directive to "none" AllowOverride None [Options, FileInfo, AuthConfig, Limit]

.htaccess files can override the sections for the corresponding directory, but will be overriden by other types of configuration sections from the main configuration files. This fact can be used to enforce certain configurations, even in the presence of a liberal AllowOverride setting. For example, to prevent script execution while allowing anything else to be set in .htaccess you can use:

Allowoverride All

Options +IncludesNoExec -ExecCGI

Another common use of .htaccess files is to enable Server Side Includes for a particular directory. This may be done with the following configuration directives, placed in a .htaccess file in the desired directory:

Options +Includes
AddType text/html shtml
AddHandler server-parsed shtml

Finally, you may wish to use a .htaccess file to permit the execution of CGI programs in a particular directory. This may be implemented with the following configuration:

Options +ExecCGI
AddHandler cgi-script cgi pl

To stop people from viewing a particular directory in your site, include this line in the .htaccess:
order allow,deny
deny from all

To stop everyone expect one from viewing a particular directory in your site add the following in
.htaccess file. Replace IP with the public IP of the one who you want to enable access to the link

order deny,allow
deny from all
allow from IP

You can use .htaccess file to redirect your website. Two examples using redirect rules are as follows

RewriteCond %{HTTP_HOST} ^.*$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

RewriteEngine On

RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule .* [r]

To redirect public_html directory to a subdirectory (for example to redirect to

put the following code in a .htaccess file in public_html directory

RewriteEngine On

RewriteRule ^(.*)$ test/$1 [L]

To execute php files in the directory as php5 code
AddHandler application/x-httpd-php5 .php

No comments: