Howto make Friendly URLs

web

Thinking and Making has a good article called: Friendly URLs improve usability and user experience. I've always been a big fan of the friendly urls, when I see a site that uses friendly URL's I get a sense of elegance, and cleanliness.

I like to use mod_rewrite with Apache (there are a number of alternatives to mod_rewrite for IIS).

Suppose you have a script called news.cfm that takes an id in the url - a typical url might be /news.cfm?id=123. Now suppose we want url's like /news/123 - you can do that like this inside your VirtualHost:

RewriteEngine On
RewriteRule /news/([0-9]+) /news.cfm?id=$1 [PT,L]

The first part of the RewriteRule is the pattern to match, this is a regular expression. The main pattern we are looking for is the id which appears after /news/. This is simply an integer so we can use [0-9]+ to match the integer. The + means that there are one or more numbers (you could use a * to mean zero or more).

You will notice that in our RewriteRule we have put this pattern in parenthesis, this allows us to use it in the url we are rewriting to (the second part of the RewriteRule) - it's called a backreference. We can refer to the back reference using $1, if you have more than one back reference use $2, etc.

The third part of the RewriteRule are some options that you may or may not need. The PT stands for pass through - it tells apache to pass the new url into other modules. This is usually needed if your depending on other modules, for instance in ColdFusion you need to pass info through to the ColdFusion apache module.

Finally the L means that if the rule matches don't check any other rules. It is usually a good idea to include this to save processing.

The article also mentions that its a good idea to rewrite url's that users may try and guess. So suppose your Apple, and you just released tiger, some people may try going to apple.com/tiger instead of apple.com/macosx. To redirect in this case you want to do a permanent redirect using the 301 HTTP status code.

RewriteRule /tiger.* /macosx/ [R=301,L]

We simply use the option R=301, this tells your browser, and other clients that /tiger should be /macosx/, your browser will make the request for /macosx/ instead, and that is the address you will see in the location bar.

I picked this example because Apple actually does redirect /tiger to /macosx/

One final note, it is usually a good practice to use the ^ character to match the beginning of the url pattern, and a $ to match the end of the url. Back to our first rule, we might use the following pattern instead: ^/news/([0-9]+)$. If you were using the first pattern, it may also match something like /foo/news/123 instead of just /news/123.



Related Entries

15 people found this page useful, what do you think?

Trackbacks

Trackback Address: 503/64CF605BF921FD64218C0AB95A3FDC74

Comments

On 01/31/2006 at 3:34:25 PM EST John Reynolds wrote:
1
Thanks for this info and the link to the other article. One benefit I didn't see mentioned was the inclusion of these rewritten URLs in web stat reports (+ google analytics). You usually have to do some extra digging or extra filtering to show stats using Query String parameters and this method would help you avoid that altogether.

On 02/13/2006 at 8:05:18 PM EST Jeff Lemmon wrote:
2
Do you know of a CF tag that can give you this same functionality? It would be helpful on a shared hosting environment. Thanks!

On 03/13/2006 at 9:33:39 AM EST Ashley wrote:
3
How do i make a url?

On 02/08/2007 at 2:56:30 AM EST Jen Farber wrote:
4
Don't forget about using the [QSA] flag in combination with the R and L flags..

Are you ever going to finish this?

Heres a helpful htaccess code snippets article: http://www.askapache.com/2006/htaccess/htaccesselite-ultimate-htaccess-article.html

On 04/16/2007 at 8:56:25 AM EDT david wrote:
5
how much is the coast of url please

On 04/27/2007 at 11:07:16 PM EDT Helga wrote:
6
Found your site in google, and it has a lot of usefull information. Thanx.

On 09/10/2007 at 4:32:16 AM EDT Sally wrote:
7

I wanted to thank you for the time you spent building this page.0

On 02/23/2008 at 1:59:17 AM EST judy wrote:
8
Check out my new site:)

On 02/26/2008 at 1:06:56 AM EST lhuv wrote:
9
Looking for information and found it at this great site...

On 05/28/2008 at 7:06:13 AM EDT john wrote:
10
Are you a big fan of movies and all the new releases on the big screen? Do you like to watch all the latest movies as soon as they are released? If the answer is yes, and you not only love to watch movies but you also like to get loads of other movie related products as well then there is a web site that is perfectly suited to you. The web site that you should consider taking a look at is called. The Films gives its visitors the chance to down load many of the latest movie releases as well as offering loads of news about all that is going on with in the movie industry and the actors in and around Hollywood. From this web sites well designed menu system you can also access movie sound tracks, and down load wall papers of you favorite movies and movie stars.

On 01/21/2009 at 7:33:03 AM EST www.hostinghebat.com wrote:
11
Great article

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?