The .htaccess File and Redirects

One often overlooked part of configuring an Apache server is the .htaccess file. This file may contain settings to alter how your Apache server behaves and reacts to various requests. The name of the file itself may look a little mysterious if you are new to Apache, and it stands for hypertext access. While the .htaccess file may contain settings for many features of Apache, the focus here is on redirection of requests.

Redirection of requests is very useful when maintaining a website. Take the case where an entire website has been redesigned and pages have new names. A newly redesigned website might make some gush that it's improved, but when there are tons of links pointing to old pages the moment the new site is deployed visitors begin getting 404 errors for old pages which do not exist in the new site leaving visitors stranded and moving on to the next result from a search.

This problem could be solved by creating a small HTML stub to perform the redirect and the code isn't hard to write and it may seem like a simple solution. Here's an example of performing a redirect with just HTML:

<html>
<head>
<meta http-equiv="Refresh" 
   content="1;url=http://www.yoursite.com/theSuperDuperNewPage.html"/>
</head>
<body>
   <p>Taking you to the Super Duper New Page!</p>
</body>
</html>

And if it was still 1999, this would be awesome. Problem with this approach is that there is yet another page to maintain, and the next update will force this page to be revisited again (and again, and again...).

Looking at this problem from an SEO viewpoint, a visitor would get bounced from one page to another and that's not a behavior which is going to win points with search engines.

Additionally, if a tool such like the Webmaster Tools from Google is used it is handy to see what URLs are giving 404s as it is quite possible that a page reference is incomplete. Take the case where for whatever reason an incomplete URL to your webpage exists; imagine http://www.yoursite.com/ind (possibly a truncated request for index.html) as the URL. There is no file for Apache to serve up that matches this request, so this is another case were a 404 would be returned. Webmaster Tools will alert when this situation exists, and it is a good practice to check for these cases on a regular basis.

Thankfully, there is a way to solve these issues without maintaining another html page and to implement the solution with one "line of code" by using the .htaccess file. The .htaccess file exists in the root directory of a site and can be edited with any text editor. Using the example about, to redirect to the "real" page, the following section must be added to the .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
Redirect permanent /idx http://www.yoursite.com/index.html
</IfModule>

The "Redirect permanent" line has two arguments. First is the pattern to match, in this case "/idx"; and note that this is relative, that is without the http://www.yoursite.com. The second parameter is the fully qualified name to the page that should be the target of the redirect: http://www.yoursite.com/index.html.

Using the .htaccess file in this way eliminates the need to use a HTML redirect, and allows addressing of invalid pages with a single entry in the .htaccess file.