Tuesday, April 21, 2009

Apache Mod_Rewrite & Shortcodes

I'm a big fan of Apache's Mod_Rewrite module. One of its benefits is that it allows me to take quite complex, user and search engine unfriendly URL's and have them mapped to ones which are easier to remember and more search engine friendly.

On a personal project I'm working on currently (I'll write more about it later), I wanted to implement a shortcode system similar to the one used for properties on www.daft.ie where a user simply has to type in a forward slash and a short code after the domain name to view a property, the code being specific to that property. Of course, short codes are also useful for posting URL's to Twitter which will also be a consideration for this particular project.

I decided I would construct my shortcodes from the characters a-k, m, n and p to z and the digits 2 to 9. Characters and digits that are easy to confuse such as 1 and l and 0 and o I discarded. This gives me a pool of 31 characters to construct my 6 character shortcodes from.

Now to map my shortcode to the full URL that displays a particular record from the database I added the following line to my .htaccess file.

RewriteEngine on
RewriteRule ^([^./]+)/?$ /$1.php [L]
RewriteRule ^([a-kmnp-z2-9]{6}).php$ /destination.php?urlparameter=$1 [L]

The first line turns the mod_rewrite module on, the second allows URL's to be reached without specifying the script extension and the third line takes my 6 character shortcode and maps it to the URL which displays the particular record the shortcode represents. This worked fine until I realised that it was also going to catch other URL's on my site with 6 letters after the domain name such as http://www.mydomain.com/search.

I finally figured out a workaround using the following rewrite condition directly before my third rewrite rule above:

RewriteCond %{REQUEST_URI} !^/search.php


Now while this works, it could be a bit of a maintanance issue as I will have to add a rewrite condition for every script I add to my root directory that has a name of 6 letters. I thought about making the shortcodes uppercase to avoid confusion with other scripts but this seems to take from the user-friendliness of the system.

Any regular expression or mod_rewrite gurus out there see a way to write my original third line to distinguish shortcodes from valid script names?

No comments:

Post a Comment