Jan 192000

Redirecting URL requests with Apache

This article shows a quick and easy way to deal with URLs that have changed to another server.  I used this for my website when I split it up into four different sites.

What does a redirect do?

A redirect allows you to specify that a requested  document is no longer at the specified URL and provide the new and correct URL.  This feature is extremely useful when rearranging a website.  I also consider such redirects to be a courtesy to the users of the website.  It is also a selfish act upon the part of the website owner.  I mean, I want you to find what you want.  So it is in my best interests to make that process as easy as possible.  A redirect is one such tool to ensure your readers find what they want.

At one time, my website contained four different domains:

  • freebsddiary.org
  • freebsddiary.com
  • racingsystem.com
  • dvl-software.com

When I split these four domains up into their own websites I wanted the original URLs to still work, as much as possible.  But people were accessing different sections of the website using the "wrong" URL.  That is to say, some of the FreeBSD Diary data was being fetched with the racingsystem.com domain.  I wanted that to still work.  At least until such time as I was able to track down some of the referring URL and get them updated.

Before the split, people could access the FreeBSD Diary via http://www.racingsystem.com/freebsd/.   After the split, I wanted that URL to still work.

Why bother with redirections?

I can think of two main reasons why redirections  are important.
  1. A redirect allows people to find the documents they want.  This is especially important when you consider the bookmarks which people have made.  If people can continue to find the documents they have referenced before, they are much more likely to continue to use your website.
  2. A redirect allows the URLs contained within search engines to continue to work.  This helps people who use the search engines to look for information.  I also suspect, but I am not positive, that the search engines will notice that the URL has changed and automatically update their records.  I say suspect because the return code is a 301 (to indicate the permanent move of a file).And more importantly, the contents of the search engines will still work.  More on the details of the 301 code later.

Example solutions

For these examples, we will use http://www.racingsystem.com/ as our website.  We will redirect some incoming requests to other sites.

A redirect statement can be placed either within virtual host section of httpd.conf or within a .htaccess file within the website.

Here is my .htaccess file located at http://www.racingsystem.com/freebsd/.   This can also be placed within the virtual host definition of your httpd.conf.

Redirect permanent /freebsd         http://www.freebsddiary.org/freebsd

As you can see, if you click on the above link, you wind up back at the FreeBSD Diary home page.  All URLs which specify the the /freebsd directory will be redirected to the URL specified at the end of the line.  Note: at that website I do even more processing to get the people to the correct URL.  See Rewriting URLs within Apache for more information.

The above was an example of a moved directory.  Now let us consider a moved file.   Here’s what I do for that instance:

Redirect permanent /services.htm    http://www.dvl-software.com/services.html

This example redirects all requests for http://www.racingsystem.com/services.htm to http://www.dvl-software.com/services.html.

Order is important

The order of redirects is important.  For this example, we are using http://www.freebsddiary.com as our website.   If you have the following two rules, it won’t do what you want.
Redirect permanent /            http://www.freebsddiary.org/
Redirect permanent /products    http://www.dvl-software.com/

the second redirect will never be invoked as the first redirect will take precedence as it matches first.  The rules should be in this order:

Redirect permanent /products    http://www.dvl-software.com/
Redirect permanent /            http://www.freebsddiary.org/

With the above order, you will be redirected to dvl-software if you ask for http://www.freebsddiary.com/products.htm and you will be redirected to this site if you ask for http://www.freebsddiary.com/.