Apache: RewriteRule
This Tutorial is for Beginners
This Tutorial will be further expanded upon in the near future. For now there is enough information to immediately allow you to elminate errors at your Web site, created from old pages you used to have and other sites are still linking to.
The Tools
Link Checker program
You need to make sure your own Web site has been checked for broken links. This allows you to know for sure, the errors are not being created by your own Web pages or scripts. Many Link checkers are available for free and pay-for. A free one that works pretty good is Xenu Link Checker and works with most versions of Windows.
Error Logs
Access to these are important as you are provided with the information you need to know, on the errors being created for your site, or hosting account. Error logs are a goldmine of information, but for the moment, we will focus on errors being generated from files not found.
.htaccess file
The ability to create or edit, the .htaccess
file is a must. Without it you will not be able to do much of anything in this Tutorial. This file is located in the Document Root
of your hosting account — same place as your Home Page. This is a regular text file and using it in any OS and any Text Editor will work just fine. The tricky part is to make sure there is no .txt
added to the end of the file name. Not a problem as once you have uploaded it, you should be able to rename it using your FTP program.
The Code
There are two parts to making the RewriteRule work properly; turning it on (once only, no "off" required) and using an "if - then" statement. Something else to be aware of, good coders always document their work. Not only for their own benefit, but also for those that may have to work with the code down the road — needing to know why you did what you did or why you did it the way you did.
This is achieved through the use of "commenting out" the documentation by using the pound or sharp sign: # at the beginning of a line. Anything to the right of it is ignored as code and you need one for each line to be commented out.
Single page
RewriteEngine On
# added such-n-such a date
RewriteRule old_page.html http://yourdomain.com/new_page.html
↑ if ↑ ↑ then ↑
# added such-n-such a date
RewriteRule dir/old.html http://yourdomain.com/dir/new.html
↑ if ↑ ↑ then ↑
The above code tells the Server that anyone looking for the "old_page.html" should instead, be shown the "new_page.html" without delay. This turns a broken link into a valid link. The same applies when it's a page in an sub-directory.
Important- Look again at the code and notice, after the
RewriteRule
there is no forward slash / — because it is not needed. The Server already knows to start looking inDocument Root
.
- We do not have to use
http://
for the "old_page.html" (the "if" part) because the Server already knows that part. We use it for the redirection (the "then" part) because you could redirect the request to anywhere you like — presuming the URL is valid.
- The complete;
RewriteRule + if + then
must be on one line.
Examples
Due to various outdated links still on the Internet and not yet updated, we still get many requests for various links we used to have. Rather than having lots of error entries and people being confused, getting our 404 Not Found page, it made sense to redirect people to a page that was created to explain.
Our Contact Us page used to be called About Us.
http://potentproducts.com/aboutus.html
A few years back, Potent Products used to provide a Newsletter.
http://potentproducts.com/newsletter/index.html
Redirecting a whole Directory
There are times when a whole directory has been moved or renamed. The above example with our Newsletters is a good one to use. Rather than making redirection code for each page, it is much easier to redirect any requests within that "no longer used" directory with just one line of code.
# added such-n-such a date
RewriteRule old_dir/.*(.*) http://yourdomain.com/nla/info.htm
↑ if ↑ ↑ then ↑
This tells the Server, any requests within the "old_dir" are to be sent to that URL without delay.
http://potentproducts.com/newsletter/index.html
http://potentproducts.com/newsletter/dir1/index.html
http://potentproducts.com/newsletter/1/7/2003/edition34.html
As you can see, all links are requesting a page in the "newsletter" directory and, because of the RewriteRule, all are being redirected to a specific page. No errors are generated and people know what happened — which is the way a good Web site should work!
Summary
With a bit of trial & error, you can now setup code in an .htaccess file, to help elminate errors on your Web site and keep your Visitors from getting confused.
RewriteEngine On
# added such-n-such a date
RewriteRule old_page.html http://yourdomain.com/new_page.html
# added such-n-such a date
RewriteRule old_dir/.*(.*) http://yourdomain.com/someother_dir/
Remember, each "if - then" statement needs to be completly on one line. If you are using a program that has Word Wrap turned on, don't worry, it will still be one line after you upload it.