Implementing 301 and 302 Redirects on Apache

MoreVisibility - March 13, 2007

In my last post titled “When and why to use 301 or 302 redirects”, I talked about why you might use these redirects and briefly explained the differences between the two. It would be a good idea to read the last post if you don’t know what a 301 or 302 redirect are because today I’m going to discuss ways to implement them.

So, you have decided that your site has a need for a SEO friendly redirect. The good news is that they are not too hard to setup on most server configurations, If you are using Apache or IIS, the tools you need are already installed and ready to go.
Since I’m a pretty big Linux fan I tend to focus on Apache, but we can’t ignore the popularity and power of IIS, so I will go over that configuration on my next post. Today though we will focus on Apache.

On Apache there are a couple places where we can put our redirect rules. The most popular place is probably in a .htaccess file, which is in the same directory as the pages you wish to redirect. The .htaccess file gives you the ability to implement configuration changes on a per-directory basis. This makes it a bit easier to edit for beginners because you don’t have to dig through a large configuration file to find where their site is defined.

The other place to perform this is at the site wide level using httpd.conf
(or Apache2.conf if using Apache 2.x). Configuration directives in this file will be used site wide.

Adding your redirects into the httpd.conf file is said to be more efficient because the file is only read in once at the server start whereas the .htaccess file is interpreted on every HTTP request. This could make a large difference if you have a significant amount of web traffic.

The downside is that it might be a bit difficult to find where you should put the
redirect rules at. I have seen .htaccess files used on many sites for the redirects without any problems, so which one you use is up to you. I would recommend playing with a .htaccess file until you get the hang of things. That’s not only because it is easier to create, but also because you don’t need to restart Apache to see how the changes worked.

Now that you understand the uses of Apache configuration files, lets get onto the examples!

Here we are performing a standard 301 redirect via Apache’s .htaccess file.
I found my website root at /home/www/mysite/. This is a good place to create a .htaccess file. Using Notepad, VI or some other standard text editor (not word) create a new file called .htaccess and place it on your server where the files you wish to redirect are at.

Inside of this file I created the line below. You can create as many lines of this as you need, each line only needs to be separated by a line break.

Redirect 301 /oldfile.html
Redirect 301 /oldfile2.html

The lines above are what I just added to the .htaccess file. This tells Apache to perform a “301” redirect from oldfile.html to You could then go a step further and test it with the excellent tool, HTTPView which I mentioned in my last post.

If you want to try this with a 302 redirect, take a look at the small difference below. Add this to your .htaccess as you did before.

Redirect /oldfile.html

See how easy that was? The only thing that changed was the “301” which no longer exists. 302 is the default for the redirect directive, so if you use no options you are performing a 302 redirect already. Keep in mind, the 301 is the SEO friendly
redirect you most likely want to use. There are certain cases where a 302 is useful though.

Implementing redirects via the httpd.conf file is a bit more difficult, but the actual directive uses the same syntax as above. The only other hurdle is finding where this should go in such a large configuration file. This time you will be editing Apache’s configuration file instead of creating a new separate file so before making any changes always backup your original copy.

Below we are performing a standard 301 redirect via Apache’s httpd.conf file.
This is exactly the same as before, only here we have to find where our site’s configuration section starts. On my server this is what I needed. I only added the line “Redirect 301 /oldfile.html“.
The section was already put in the file when my web site was first setup.
Redirect 301 /oldfile.html
If you have trouble finding your httpd.conf file, here are some places that seem to be pretty common on most Linux distributions. My Apache install was at /var/httpd/ so the httpd.conf file can be found in /var/httpd/conf/httpd.conf. This is a very common place to find your apache files. Another place to try is /var/apache or /var/apache2 which is used often as well. This is the extent of what is involved when it comes to performing server level redirects with Apache. I will cover IIS and PHP/ASP based redirects on my next post.

© 2024 MoreVisibility. All rights reserved.