Creating a 301 Permanent Redirect in Apache

The Redirect Solution
Ever run into so many 404 “Page Not Found” errors while browsing the web that you started to see red? If so, you are certainly not alone. Running into a dead end when you were expecting to see useful, relevant information can be very frustrating. Considering the number of 404 errors that exist on the web, you may be surprised to learn that there’s actually a pretty simple way to redirect users when you move a page, or even when you delete one.
This solution is something called a 301 Permanent Redirect. Unlike those aggravating 404 errors, the 301 will actually send users to a URL you specify. It will also let Google and other search engines know that you’ve done this, so they can update their own records and send users to the right place first time around. The best thing is that you don’t have to mess around with javascript to create a manual redirection page—search engines will do all the work for you.
Although there are ways to do this regardless of which server software you’re using, this tutorial is only going to cover how to create a 301 redirect in Apache.

Getting Started
Before you can create the redirect itself, there are two things you need to make sure are turned on in your Apache install. The first of these is something called Mod Rewrite. To turn this one on, simply:
• Open your Apache configuration file (httpd.conf) in a text editor of some sort
• Look for a line that says LoadModule rewrite_module modules/mod_rewrite.so
• If it’s already there, you’re done with the httpd.conf file
• If the line does not exist, just add it to your configuration file
Once Mod Rewrite is turned on, you need to check one other thing before you can create your redirect page. Close your configuration file and look for another file, this one titled .htaccess. The .htaccess file is usually in your server’s root directory. If you don’t already have one, you can create a blank file and rename it .htaccess. You will need .htaccess to make the 301 permanent redirect work, so you will have to create one in any case.
In the .htaccess file you are going to look for a line that says:
• RewriteEngine ON
• If you don’t see this, add it to the file
Now you’re ready to redirect some web pages!

How to implement a 301 Redirect
One of the most useful things about the 301 permanent redirect is that it can be used in a variety of situations. You can redirect a single web page, leave the page names the same but change the directory name, redirect pages and directories, or flat-out move your entire existing website to a new domain. Regardless of how you use the 301 permanent redirect, you’ll need to keep that .htaccess file open—that’s where you’ll be making all your changes to put the redirect into effect.
Simply find the type of permanent redirect you need in the list below and put the code a few lines below where you added the Rewrite Engine code, and you should be in business.

Redirect Types and Code
• Redirecting a single page:
Redirect 301 /originalURL.html http://www.mysite.com/newURL.html
(note that this all goes on one line)
• Adding WWW to the front of your URL:
RewriteCond %{HTTP_HOST} ^WebSiteURL.net
RewriteRule (.*) http://www.WebSiteURL.net/$1 [R=301,L]
(two lines of code)
• Deleting an old directory and redirecting to a different one:
redirectMatch 301 ^/photocopies/ http://www.WebSiteURL.net/oldpages/
(one line of code)
Note that you can also redirect to a single page instead of a new directory:
redirectMatch 301 ^/photocopies/ http://www.WebSiteURL.net/scans.html
(again, one line of code)
• Changing the name of a directory to something else:
redirect 301 /photocopies/ http://www.mysite.com/scannedimages/
(one line of code)

Changes to your .htaccess file take place instantly, so after you’ve added a rule, save the file and refresh to page to make sure it’s worked. Those annoying 404 pages are gone forever!