The FreeBSD Diary
Providing practical examples since 1998If you buy from Amazon USA, please support us by using this link.
Redirecting URL requests with Apache 19 January 2000
|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:
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.
|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
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/.