Apache: Redirecting users

By | April 22, 2006

Today you’ll be learning about the power of internal and external redirects that you can make using Apache. Why, because you can! No serious if you are running a dynamic website those URLs aren’t really user friendly now are they.

This is sort of a follow up on one of my previous posts about how to set up a webserver using Apache. So lets introduce you to what we’re going to do, its the magic called mod_rewrite.

Simple example
Lets start of simple. Say you have a very popular page that will be moved to a new location after some redesign of the website. This is posing a big problem, you have a lot of visitors on your page and search engines seem to like it. But if you move it you will loose the traffic from your visitors and search engines seem to hate you for it, just keep in mind it’s nothing personal. So lets do some magic shan’t we, make a file called .httaccess and add the following line:

RewriteRule old.html new.html [R,L]

So what did we just do, well two things we told the server to redirect any visitor requesting old.html to new.html and we told the server this would be the last rule to be applied. In other words it would stop loading and immediately send the information to the visitor.

Making URLs more user friendly
The next step in rewriting and redirecting is building a set of rules to use for a dynamic website. Why? Because it is easier for the visitor to remember and search engines like them more. Most of the websites currently on the internet are dynamically generated, which also means uggly URLS like page.php?id=1&page=3&section=34&blah=gun. Not really worth the effort of remembering now is it!

Lets order Apache to rewrite a cleaner for of this URL to the nice URL that is in the example:

RewriteRule (.*)/(.*)/(.*)/(.*).html page.php?id=$1&page=$2§ion=$3&blah=$4

Did you notice how simple that was. Now keep in mind this URL is still not visitor friendly, but we are getting there. In an optimal situation you could remove the ugly numbers and replace them with text. Something like: New York/Harlem/Sales/Guns.html.

Redirecting bad visitors
All websites have them visitors that just don’t want to obey the rules set by the webmaster. After multiple warnings you just got sick of them and decided to block them based on IP. Here again Apache can help you somewhat, please not that this is not good if you expect huge lists. In the case of long lists you’re probably better of letting the server side script dealing with them. Anyway I’ll show you how to block someone based on IP.

RewriteCond {%REMOTE_ADDR} "10.133.221.*"
RewriteRule .* http://www.google.com [R,L]

I did not just block one user though. I just blocked every visitor that comes from the range of IP to, just love that Apache!

Redirecting textual browsers
Another good reason to perform redirects are to give visitors a page they can actually see. There are still some people out there that use a text only browser to surf the web. Sure your page looks good, but for them it might be a nightmare. So lets redirect them to a low-fi version of the website shan’t we.

RewriteCond {%HTTP_USER_AGENT} "Lynx.*"
RewriteRule (.*) low-fi/$1 [NC,L]

Again I’m looking for the user agent Lynx, which is a textual browser, and I am redirecting every request to a low-fi address. Here’s the sweet part, they don’t even now it’s being done! These redirects are all internally and never get to the visitor. All they see is the proper version of your website.

Enjoying the redirects and the power of Apache, well go ahead and play some with it. This post is ending so I’ll see you next time I guess.

Leave a Reply