Using .htaccess

Is .htaccess enabled?

To test whether .htaccess is enabled on your web server, create two files with unique content and add the following to your .htaccess file (if the leftmost file is loaded, then .htaccess will be enabled):

DirectoryIndex index_enabled.html index.html

Error pages

404 error — “page not found”:

Redirect 404 /path/to/404-error-page.html

301 error — “moved permanently”:

Redirect 301 /path/to/old-page.html /path/to/new-page.html


Redirect serves the browser a different URL to use, and the browser is aware of the redirect. Rewrite is a set of conditional rules that leaves the client’s browser none the wiser of the rewrie (in contrast to Redirect’s if EXACTLY THIS do EXACTLY THAT nature).

A Rewrite request is made up of two parts—RewriteCond and RewriteRule. RewriteCond defines when the RewriteRule should be used and RewriteRule defines what is to be done.

Replace non-canonical URLs with a canonical URL (this is actually processed as a 301 redirect):

RewriteCond %{HTTP_HOST} !^www
RewriteRule .* http://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Remove any trailing slashes from the URL:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

Remove index.html from the URL:

RewriteCond %{REQUEST_URI} ^.*/index.html
RewriteRule ^(.*)index.html$ /$1 [R=301,L]

Set rules for search engine robots:

<FilesMatch "sitemap\.xml">
  Header set X-Robots-Tag "noindex, nofollow"

Further reading

Apache HTTP server

Google Robots Meta Tag

Back to top Back to Home