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} !www.newsite.com$ [NC]
RewriteRule ^(.*)$ http://www.newsite.com/$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} www.oldsite.com$ [NC] defines the condition the rule(s) should be run under. In this case we are saying the when the entered URL is not (!) www.newsite.com. The [NC] makes the condition not case sensitive.
  4. RewriteRule ^(.*)$ http://www.newsite.com/$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 www.newsite.com, and anything typed in addition to the base domain is appended to www.newsite.com 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} subdomain.parentdomain.com$ [NC]
RewriteRule ^(.*)$ http://parentdomain.com/$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} !newdomain.com$ [NC]
RewriteRule ^(.*)\.html$ http://newdomain.com/$1\.php [L,R=301]
RewriteRule ^(.*)\.php$ http://newdomain.com/$1\.php [L,R=301]
RewriteRule ^(.*)$ http://newdomain.com/ [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 ^(.*)$ http://newdomain.com/ [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 http://markhnichols.com/
Redirect 301 /blogs/cat_life.html http://zanshin.net/category/life/
Redirect 301 /blogs/001398.html http://zanshin.net/2007/12/29/moving/

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 http://newdomain.com/ redirects visitors looking for my resume at my old site to its new location at a new site entirely.
  2. Redirect 301 /blogs/catlife.html http://zanshin.net/category/life/ redirects visitors looking for a category page using the old location (/blogs/catlife) to its new home (http://zanshin.net/category/life/).
  3. Redirect 301 /blogs/001398.html http://zanshin.net/2007/12/29/moving/ redirects visitors using a old link to a posting (/blogs/001398.html) to its new location (http://zanshin.net/2007/12/29/moving/).
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.