Home : Internet : Server : Apache : htaccess


.htaccess files are a type of "distributed configuration file", meaning that they provide a way to set configuration options in a distributed manner. Specifically, .htaccess files allow you to set rules (such as access permissions and page redirection) to individual folders within a website or server directory. When a .htaccess file is placed in a directory, the defined rules apply to that directory and all of its sub-directories.

Here are some of the things htaccess can do:

Notes about .htaccess


This is a simple example of a .htaccess file:

# Enable URL re-writing:
RewriteEngine on
# Redirect users to "www" if necessary:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Permanently redirect a file within the same domain:
RedirectMatch permanent ^/path/to/old-file.html$ http://www.example.com/path/to/new-file.html
# Permanently redirect a file to a new domain:
RewriteRule http://www.old-domain.com/old-file.html http://www.new-domain.com/new-file.html

# Parse html files as if they were .shtml:
AddHandler server-parsed html htm

# Define your preferred default index pages (the last option redirects to the site root index page):
DirectoryIndex index.html index.htm default.htm index.php index.cgi index.pl /index.html

# Prevent certain browsing agents from stealing your stuff:
RewriteCond %{HTTP_USER_AGENT} Curl [OR]
RewriteCond %{HTTP_USER_AGENT} HTTrack [OR]
RewriteCond %{HTTP_USER_AGENT} WebCopier [OR]
RewriteCond %{HTTP_USER_AGENT} Wget
RewriteRule ^(.*) - [F]