book musings



missle defense



cultcha blog


scripts & programs

on the town



creds (PDF)



Apache: Redirecting Web Page Requests

If you move a Web page on your site to another location, or give it a another address, there are a number of ways you can have the Apache Web server software automatically redirect browser requests that come in for the page to the new location.

The easiest way is to put a page at the old address that automatically directs the browser to the new location, i.e.:

<meta http-equiv="refresh" content="0;url=http://www.TheNewAddress.com"> 
In the above page, the meta tag redirects the browser to the new location (here, it is "http://www.TheNewAddress.com"), with a delay of 0 seconds ("0") . The user just sees the page at its current location.
This process of setting up a new page for each updated address is a but cumbersome though. Far better would be to put all the old addresses and their new replacements in a single file, which Apache could check every time a new request for a page comes in.

Fortunately, the HTTP protocol has something called 301 Status code, which is basically a permanent change-of-address notification.

For Apache, doing a 301 redirect involves setting a .htaccess blank file (or appending an existing one). The period in front of the the name means it will be a hidden file---to see hidden files, use the "ls-a" command.

To create such a file, just name a blank text file .htaccess. Place it in the root directory of your Web server (Or if all the pages you are redirecting are in one directory, place the file in that directory).

Then, add a new line for each redirect in the following form:

[old address] [new address]
For example, this entry at the bozo.com site...
/OldFiles/OldBozo.html  /NewDirectory/NewFile.html
...clicking on the link "http://bozo.com/OldFiles/OldBozo.html," the user's browser will automatically pull up the "http://bozo.com/NewDirectory/NewFile.html."

Note that when the new page is outside the control of the Web server, the full address (including "http://") of the destination address must be used, not just the internal directory tree.

Setting up an Apache .htaccess file, if one didn't previously exist, requires letting your copy of Apache know that this file exists and should be consulted. In Ubuntu, and probably other distributions as well, Apache ignores the .htaccess page in the default install.

(Note, for this instruction, I am using Apache 2.2.8 on Ubuntu server 8.0.4).

Doing this requires two steps. First of all, find the "apache2.conf" file. In Ubuntu, it is located in "/etc/apache2" directory. It can be edited at the command line with a text editor, such as vi, emacs or Pico.

Open the file and search for the mention ".htaccess." Check to see that ".htaccess" follows the "AccessFileName" option. If it is enabled, there will be no '#' at the beginning of the line (meaning it is not commented out). This tells Apache to look in this file for directives, such as a page address substitute as the one above. It should read:

AccessFileName .htaccess
That is probably already set correctly, but the second step probably involves some changes in configuration. Namely you have to set something called "AllowOverride," which is the configuration setting that tells Apache whether or not to follow the .htaccess requests

This option can be found in another file, one showing the directories that Apache should use for the Web site. In Ubuntu, it is the "default" file in the "sites-available" folder ("/etc/apache2/sites-available"). (NOTE: In Ubuntu this file is also under another name as a symbolic link, in the "sites-enabled" folder.)

In this "default" file, you will find a list of directories on your server that have been enabled as Web server pages.

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
NOTE: This is the not the "document root" entry, but the one right after it. The "document root" entry also has an AllowOverride. It is set as "none" and can stay that way.

Each entry (framed by <Directory> and the </Directory> tags) in this list specifies the options that Apache should use for that directory. In the above entry, change "AllowOverride None" to "AllowOverride All".

A bite of explanation: "AllowOverride None" means Apache does not look for the .htaccess file, and does not follow its instructions. "AllowOverride All" means that it does.

After you make this change, or any changes to these configuration files, you need restart the Apache server software. In Ubuntu it is done thusly from the command line:

/etc/init.d/apache2 restart
(Note, you do not need to restart Apache when new entries to .htaccess are made. That seemed obvious but I should mention this anyway)
Also, if you know that all your rerouting is being done from one folder. You can place the .htaccess file in that folder, and, instead of changing the "AllowOverride" setting for the whole site, just make a new entry in the "default" configuration file for that one directory.

For instance, I wish to redirect addresses of expired Web pages in the "/var/www/L/" folder. I would place an .htaccess file in that folder and add this entry into the "default" configuration file:

  <Directory /var/www/L/>
                AllowOverride All
This seems to be all you need to add--the other options are inherited from the listing of the parent directory.

End-note: I've found that Apache is extremely fussy about what is put into an .htaccess file. Don't put junk in just as a way of testing something else out. Only properly formed URL's or internal links should be added. Anything else will halt all Apache redirects, giving users only error messages.

Note: Other forms of redirection are discussed here.