Advanced HTML Guide

.htaccess Configuration Files

Basics of .htaccess

.htaccess files are text files that you place in directories of your Apache web server to configure a variety of things such as custom web pages, redirects, password protection and allowing or dissalowing access to your site via IP address. Alghough .htaccess files are very common your ability to use them depends on 1) your site being hosted on a Apache server (which most are) and 2) your web host allowing you to use them. Not all hosts allow these files as they give access to some pretty powerful features but larger hosts such as 1&1 (where this site is hosted) do allow them.

The file must be named '.htaccess' and can be in the root of any directory. .htaccess files will apply settings to the current directory and all sub-directories. However the .htaccess in the current directory will override any settings in any parent directories. If you find it is not possible to initially create a file called '.htaccess' in Windows then you can create it as 'htaccess' and rename it after you have uploaded it to your web server.

Custom Error Pages

One of the most common usages for these files is to replace the default error pages. The most common error page being '404' when a file is not found. The below snippet sets the error page for a number of common error codes and preserves the error code in the HTTP header.

ErrorDocument 401 /401error.htm
ErrorDocument 403 /403error.htm
ErrorDocument 404 /404error.htm
ErrorDocument 500 /500error.htm
Note that you must not use a full URL (starting with http://) as the error page otherwise you could end up changing the 404 'Not found' error code into a 200 'OK' status code. If you return 200 as a status when you should be returning an error you could end up causing problems for search engines.

Don't do the following:

On a number of other websites I've seen the following suggested as a way of setting up custom error pages. If the file or directory can't be found it redirects to 'errordocument.html'. This is a bad idea as it changes the error code into a 200 'OK' status code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /errordocument.html

Permanently Redirecting People To Your New Page

If you move pages around on your site and you don't want to lose traffic from existing links to your old pages then you can use a .htaccess file to redirect people to your new pages. Here is part of a .htaccess file which I use to redirect people from my old domain to this one.

redirect 301 /html/advancedhtml.htm
redirect 301 /html/animbuttons.htm
redirect 301 /html/basictags.htm
redirect 301 /html/colours.htm

Redirecting People Based on the Referring Site

I faced a problem where large numbers of MIDlet game sites were hosting my game 'Atomic'. Most of them had a link to my site however they had a link to the root domain rather than the main Atomic page. I used a .htaccess file to redirect people from these specific sites to the correct page. First is how to do it for a single site. This redirects any links from '' which are coming into my 'index.html' page to the correct '' page.

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^ [NC]
RewriteRule index.html [R,L]

Next how to do the redirect for multiple sites. Note the 'OR' which says that the rule continues on the next line. The [NC] means that it is not case sensitive.

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^ [NC,OR]
RewriteCond %{HTTP_REFERER} ^ [NC,OR]
RewriteCond %{HTTP_REFERER} [NC,OR]
RewriteCond %{HTTP_REFERER} [NC,OR]
RewriteCond %{HTTP_REFERER} ^ [NC]
RewriteRule index.html [R,L]

Using redirects to ensure consistent links

The same pages on you site may be accessible from multiple URLs. This is not desirable as it can result in search engines thinking you have duplicate content. For example all the pages on your site may be viewable from both a 'www' and a non 'www' address. And the home page on your site could be vieable from '/' or '/index.html'. This could for example result in the below URLs all pointing to the same page:
This can be fixed using the .htaccess file, so that the URLs that you don't want all do a 301 permanent redirect to the correct URL. In the below example I want all the URLs to start with 'www' and I don't want my home page to show the 'index.html'.
RewriteEngine On

# Force all URLs to begin with 'www'
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

# Redirect any requests to index.html to /
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$$1 [R=301,L]

Password Protection using .htaccess

See my password protection using .htaccess page.

Advanced  HTML
Privacy Policy
Advanced HTML Home
Copyright © 1997 - 2024
Hosted by IONOS