tadhg.com
tadhg.com
 

Why I Like RewriteRule

16:45 Sun 19 Nov 2006
[, , , ]

I’ve known about Apache Rewrite functionality for quite a while, but have only recently started to think about how much better it can make web applications.

I like friendly URLs. I fully support the trend over the last several years to make URLs more comprehensible to the end user. I also hate dead links and think that web designers and developers should simply never tolerate them. Rewrite rules can help with both of these areas.

Rewrite rules take a request from a browser and then “rephrase” that request before giving it to the server. The browser never sees the change (making rewrites different from redirects). So you could have a rewrite rule to make http:/my.domain.tld/somereallylongfilename.php?q=WithSome&p=Ridiculous&p2=Parameters&numExpl=AndNumbersThrownInAsWell&nums=200611191559 accessible as http://my.domain.tld/RidiculousExample. The latter seems a lot easier to remember…

You can do this in Apache by setting up RewriteRules, which can go in httpd.conf (including in VirtualHost declarations) or in .htaccess files. I prefer .htaccess files. Basic RewriteRule syntax looks like this:

RewriteEngine On
RewriteRule [Stuff to match] [Stuff to insert instead] [other parameters]

(Although it might seem otherwise due to linebreaks below, every individual RewriteRule must go on one line.)

Here’s how to do our example from above:

RewriteEngine On
RewriteRule ^RidiculousExample$ /somereallylongfilename.php?q=WithSome&p=Ridiculous&p2=Parameters&numExpl=AndNumbersThrownInAsWell&nums=200611191559$

To make it even easier on the user, we could add that case doesn’t matter:
RewriteRule ^RidiculousExample$ /somereallylongfilename.php?q=WithSome&p=Ridiculous&p2=Parameters&numExpl=AndNumbersThrownInAsWell&nums=200611191559$ [nocase]

If for whatever reason you change your directory structure, you can use RewriteRule to move your users to the new location:
RewriteRule ^/olddirectoryname(.*) /newdirectoryname$1

(If you’ve moved the files permanently, you should probably do:
RewriteRule ^/olddirectoryname(.*) /newdirectoryname$1 [redirect=permanent])

A further advantage: if you use RewriteRule early, you can abstract the structure to an extent that allows you to change your underlying code without disrupting the user or creating 404s (404s BAD!). I’ve just started down this path with sfmagic.org, and I like it a lot. Some examples of the very ugly URLs I use, followed by their now far better “friendly” accessible versions
/show_results.php?year=2006, /2004.
/show_results.php?superset=MRD, /MRD or /Mirrodin (and casing doesn’t matter).
/show_results_playerl10_print3.php, /seedingprint.
Also, all player pages, with ‘real’ URLs like /show_player.php?playerid=23, allow access via player names like /Tadhg O’Higgins. The RewriteRule for this is:
RewriteRule ^(players/)?Tadhg[\ ]*O’Higgins[/]*$ /show_player.php?playerid=23 [nocase]

I like that, but really appreciate the freedom that it gives me—now, if I changed to an entirely different technology and structure, the URLs for player pages would remain the same. I’d simply have to change the rule to reflect this. Moreover, when I change the underlying “real” URL, I could do something like this to encourage browsers and search engines to understand which is the canonical URL:

RewriteRule ^/show_player.php\?playerid=23$ /players/Tadhg\ O’Higgins [redirect=permanent]
RewriteRule ^(players/)?Tadhg[\ ]*O’Higgins[/]*$ /playerDetails.php?id=23 [nocase]

The first line tells the browser that /players/Tadhg O’Higgins has superseded show_player.php?playerid=23 as the canonical URL. The second line does the standard rewrite as explained above. (With as many players as the group has, I should probably consider using RewwriteMap rather than lots and lots of RewriteRule lines.)

In essence, I love RewriteRule (and its companions) because it allows a web developer to shield the end user from the messy details, whether those details involve terrible URL strings or potential broken links.

« (previous)
(next) »

2 Responses to “Why I Like RewriteRule”

  1. Niall O'Higgins Says:

    mod_rewrite is very powerful, but the syntax is pretty damn esoteric. It took me a day or two to figure out the syntax of how to redirect someone conditionally, based on whether or not they had a certain cookie. And even then there are odd interactions with ServerName so that you are forced to provide an absolute URL for redirects…

    All in all, I would say that mod_rewrite is a nice tool, but horribly difficult to understand in a config file. A kind of black magic.

  2. Tadhg Says:

    Yes, you can definitely do extremely esoteric things with mod_rewrite, but that’s one of the things that makes it so useful! Agreed, a kind of black magic, but sometimes that’s what you need to get things done.

Leave a Reply