Fun with .htaccess

January 15, 2008

Recently I have made good use of the Apache .htaccess file to redirect and rewrite URL’s to keep visitors from getting page not found errors. Moving three domains, creating a subdomain or two, and then moving things around again, has created ample opportunity for lost visitors.

There are lots and lots of pages in the Google search results for “.htaccess,” so here are my notes, all in one place, for your reading pleasure.

Redirect visitors from an old site to a new site

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]
  1. RewriteEngine On is required to make all the following lines work.
  2. RewriteBase / indicates we want to work within the current directory
  3. RewriteCond %{HTTP_HOST}$ [NC] defines the condition the rule(s) should be run under. In this case we are saying the when the entered URL is not (!) The [NC] makes the condition not case sensitive.
  4. RewriteRule ^(.*)$$1 [L,R=301] is the actual rule to be run when the above condition is met. The ^ and $ define the beginning and ending of the URL the visitor has typed. The period (.) indicates any character in a regular expression, and the splat (*) says any number of characters. The visitor is sent to, and anything typed in addition to the base domain is appended to via the $1. [L,R=301] says that when this rule is executed is should be the last rule run, and that the redirect should be considered permanent (301).

Redirect visitors from a subdomain to a parent domain

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]
  1. The only difference in this example is that the condition is looking for the subdomain, and the rule states the parent domain.

In both of the examples above, it is assumed that none of the pages within the site are changing their URL format. The RewriteRule is merely appending the page specific portion of the URL to the end of the new URL.

Redirect visitors using .html links to .php links

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !$ [NC]
RewriteRule ^(.*)\.html$$1\.php [L,R=301]
RewriteRule ^(.*)\.php$$1\.php [L,R=301]
RewriteRule ^(.*)$ [L,R=301]
  1. The example above not only redirects visitors from the old site to the new, it rewrites any links entered ending with .html to end with .php.
  2. Also the last rule, RewriteRule ^(.*)$ [L,R=301], ensures that any visits to the root domain will be properly redirected. The two preceeding rules only catch URL's entered that have a page link specified.

Redirect visitors with old URL format to new URL format

RewriteEngine on
Redirect 301 /resume.php
Redirect 301 /blogs/cat_life.html
Redirect 301 /blogs/001398.html

When you change the format of the URL link (beyond just changing the link type as in the previous example) you could do it as I have above.

  1. Redirect 301 /resume.php redirects visitors looking for my resume at my old site to its new location at a new site entirely.
  2. Redirect 301 /blogs/cat_life.html redirects visitors looking for a category page using the old location (/blogs/cat_life) to its new home (
  3. Redirect 301 /blogs/001398.html redirects visitors using a old link to a posting (/blogs/001398.html) to its new location (

I make no guarantees about your success using my examples. These redirects work for me, your mileage may vary, void where prohibited, for external use only, and see your doctor.

Author's profile picture

Mark H. Nichols

I am a husband, cellist, code prole, nerd, technologist, and all around good guy living and working in fly-over country. You should follow me on Twitter.