30 December, 2010

Basic url rewrite example

URL that is readable looks nice. But despite efforts by developers, sometimes for instance, using a Content Management System (CMS) serve url's in the format like,

http://www.testthis.com/list_products?cat_name=Shirts&cat_id=8


Now, this is really a bad url, and is prevelant with dynamic pages. Some of the problems of such url's are:
1) It exposes the type of data, query string to malicious users.
2) The characters such as question mark and ampersand can be troublesome.
3) Such url's wont be indedxed by search engines.

Solution to this is, above url can be mapped to something like this;

http://www.testthis.com/products/Shirts/8


Looks much better. And in order to do such mapping, we need url rewriting.
Apache's mod_rewrite gives us the ability to rewrite urls. Url rewrite can be
done to redirect from old to new address, clean up dirty url's, as mentioned above.
This makes our url's search engine friendly which means search engines can index such urls.

For url rewrite, we create a file .htaccess at the root of our project, and add following line.

RewriteEngine On


Basic Redirects

Lets say we moved all of our files from an older location to a new one, now we want to redirect all the links to current location. For that we can do the following.

RewriteEngine On
RewriteRule ^old\.html$ new.html


What the above rule does, is, it simple redirect from old.html to new.html page.
The ^ sign indicates Start of the url to be matched. If this character is removed, then our rule would also match hold.html
The $ sign indicates end of the string to be matched. In this case users would not know a redirect has occured from old.html to new.html

But lets say you want users to know that redirect has occured, that means you want to force a redirect. In such case you can do as,

RewriteEngine On
RewriteRule ^old\.html$ new.html [R]


Using Regular Expressions in Url Rewrite
The full strength of mod_rewrite can be felt at expense of complexity. Using regular expressions for url rewrite, you can have rules for set or urls, and have redirection for all to actual pages.
For example,

http://www.testthis.com/list_book?bookId=20


Now, we can rewrite this kind or url to make it friendly, as,

RewriteEngine On
RewriteRule ^book/([0-9][0-9])/$ list_book.php?bookId=$1


So above rule will create urls such as, http://www.testthis.com/book/8

But if a user types in like, http://www.testthis.com/book/8, our url rewrite rule wont work, as the slash at the end is missing, so to prevent such problems, we can do as,

RewriteEngine On
RewriteRule ^book/([0-9][0-9])/$ book/$1/ [R]
RewriteRule ^book/([0-9][0-9])/$ list_book.php?bookId=$1


Now, if a user enters something like book/12, our first rules comes in to add a slash at the end,then second
rule comes into play.

Regular expressions in url rewrite can be expanded by using modifiers, that allow you to match url with indefinite number of characters. Lets say, our url is like,
http://www.testthis.com/list_book?bookId=220

Our rule wont match this, as we have checked against two digits only, so we should use modifiers in this case.

RewriteEngine On
RewriteRule ^book/([0-9]+)$ book/$1/ [R]

+ indicates one or more of the preceeding characters or range.
* means 0 or more preceeding characters or range

So, above rule will match both book/1 and book/3000

In this way we can use mod_rewrite to rewrite url. These are just an introductory examples. There's a lot more way to move ahead in url rewriting and regular expressions. I hope this basic url rewrite post can be of some help to others.

1 comment:

Anonymous said...

Great content again, It was really helpful. Keep Posting