Wednesday, December 05, 2007

How to get friendly urls

URLs are face of an website, the urls are indexed by search engines. They are the one links your website. if your urls change, you lose any search ranking advantage you have.

With the technology behind websites changes often, what happens to the urls?

The items list page of an shopping site changes like:

www.shopping.com/shoppinglist.html

www.shopping.com/shoppinglist.jsp

www.shopping.com/shoppinglist.php

www.shopping.com/shoppinglist.jsf

www.shopping.com/shoppinglist.seam

Also most urls are not readable, or too long.. with parameters being added to the url. http://shopping.com/list.jsp?itemid=1234

it would be more readable if it were: http://shopping.com/list/item/1234

the advantages here is not just readable urls, you are also abstracting our urls from implementation. it protects your technology changes, protects from http get (params in url) or http post implementation..

Samples:

http://mail.google.com/mail/#inbox

http://mail.google.com/mail/#sent

also you can have permalinks like http://shopping.com/deals/today

Enough talked on the advantages, let see how we can get that working.

Apache http server has a module named: mod_rewrite http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html which can transform between urls, by url rewriting. It's basically can match any expression on incoming url, and replace with different pattern. It uses regular expression to find and replace patterns.

There is a similar implementation in java, which is http://tuckey.org/urlrewrite/

These modules are efficient, and at the same time complex to learn. So always you could implement your own url rewrite module.

The web.xml can have servlet mapping with url-pattern=/ which acts as front controller and redirects to appropriate url.

There are some limitations in this approach.

to be continued..

Recommended Blog Posts