How To Do URL Rewriting

What is URL rewriting?
Well.. URL rewriting is simply a technique used to convert complicated URLs in simple and straight forward manner. This is actually used in websites which are made to dynamically update its content. For eg..  sites coded with PHP, ASP, JSP etc.
If you are familiar with programming, the complicated URLs are referred to as query URLs. They generally look like this www.somesite.com/product.php?id=58.
Here product.php?id=58 is called the query string. In this case the query string is small so it wont be problem to remember but if the query string becomes too large like
www.somesite.com/product.php?category=4&subcategory=1&id=58 the URL becomes ugly. Now here is when URL rewriting comes into use. By using this URL rewriting method we can make the URL look simple and straight forward. Now by using URL rewriting we can change it to
www.somesite.com/category4/subcategory1/product58. Easy to remember right?
Apart from this advantage it has its own importance with SEO. These URLs are also search engine friendly. Search engines generally don’t index the query URLs properly or sometimes they don’t even get indexed. The reason behind this is that search engines consider that the page would be dynamically updated and will have different results each time even though in many case doesn’t. When the URL is rewritten as static URLs with an extension of .html or .htm, the search engines consider the page as static and don’t  mind indexing them pretty quickly. So that was most of the URL rewriting now lets talk how to use this technique.

How to do URL rewriting for SEO using .htaccess file?
In this post I will explain how to do URL rewriting on an Apache Web Server.
First of all go to the root directory of you website then create a file in there and name it .htaccess
Creating this file isn’t quite very straight forward. You need to open a text editor and save is as .htaccess
Now come the important part. Add these lines to you .htaccess file.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

Then you set the rewrite rules in the following format:
RewriteRule ^([^-]*).html$ post.php?title=$1 [L]

^([^-]*) represents the value you pass to the query string which in this case is title of a blog post.
Let me elaborate a bit.
Consider a blog post with title “How to do SEO”. This page can be accessed using query URL: www.somesite.com/post.php?title=How to do SEO.
But when we use URL rewriting as above, the URL will be like this:
www.somesite.com/how-to-do-seo.html

If you want to know more about the technical details about URL rewriting, you can visit the Apache Website for detailed documentation otherwise the above code just does the trick.

You can also visit http://www.generateit.net/mod-rewrite/ to generate rewriting rules for your .htaccess file. This tool is quite convenient.
Anyways here is the full code again(Skip the #s):
##############################
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^([^-]*).html$ post.php?title=$1 [L]
##############################

Quick tip: If some of you may not be able to make it work, go to your apache configuration and check if rewriting module is turned on. Change following line:
#LoadModule rewrite_module modules/mod_rewrite.so
TO
LoadModule rewrite_module modules/mod_rewrite.so

For IIS user there is a module available on IIS website, just install it and open it. Once you go in there everything is easy.
Visit:  http://www.iis.net/download/urlrewrite
Anyways I will discuss it in upcoming posts.
 
How does URL rewriting work?
What actually happens is when browser requests a URL the web server(Apache/IIS) passes that URL to the URl rewriting module and then the module does all the stuff. It takes in the URL compares it with the pattern defined in .htaccess file and access the data internally by query strings. Then the output is fetched and displayed on your screen at a nice looking URL 🙂

If you find this content useful, don’t mind sharing it, it can make someone’s day too 🙂