Say your message board on your blog uses the following request URL to find and display the correct data:

http://www.mysite.com/blog/index.php?category=news&year=2012&month=1

This is a rather simple request, but it is not very nice looking or easy on the eye, and it can be improved, for the user end experience.

Take this for instance:

http://www.mysite.com/blog/news/2012/1

That’s much nicer, and easier to read and understand even for a non technical person. They may understand the category, year and month without any input from you,

How do you convert your site? Well you don’t need to, but you can use a .htaccess mod_rewrite to change the url in the background.

RewriteRule blog/([a-z0-9]+)/([0-9]{4})/([0-9]) /blog/index.php?category=$1&year=$2&month=$3 [L]

This takes the first quantifier after blog (a-z0-9) and says any characters, put into $1. The second is a 4 digit number, and the third, any number.

The [L] tells the re-write engine that if this rule matches, don’t do any more rule processing (last one)

If you implement this way of displaying your links, then your site should be better off in the big wide world of search engines. A lot of search engine spiders ignore URL’s with & in them as it can cause recursive looping and its easier to just ignore them.

Also, if you come to rewrite your blog in a new language, ASP for instance (shudder at the thought) then you can change the rule simply:

RewriteRule blog/([a-z0-9]+)/([0-9]{4})/([0-9]) /blog/index.asp?category=$1&year=$2&month=$3 [L]

Thinking about your user experience, even if you are considering the search engines, is important.

If you want to read more about the Apache re-write module, you can go here

By admin

One thought on “Using mod_rewrite for clean URLs”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.