mod_rewrite: A Beginner’s Guide to URL Rewriting
For Advanced Users
I mentioned user-friendliness in the introduction, and haven’t dealt with it. First, let’s imagine we’re having a huge download site that has the downloadable software separated into categories, each with a unique id (which is used in the SQL SELECTs). We use links like open.php?categoryid=23487678 to display the contents of a category.
To ensure that our URLs were easily memorized (eg. http://www.downloadsite.com/Nettools/Messengers) we could use:
 RewriteRule ^/NetTools$ /test.php?target=3  Â
 RewriteRule ^/NetTools/Messengers$ /test.php?target=34
assuming the ID is 3 for the NetTools category and 34 for Messengers subcategory.
But our site is huge, as I’ve mentioned – who wants to hunt down all the IDs from the database, and then edit the config file by hand? No-one! Instead, we can use the mapping feature of mod_rewrite. Map allows us to provide a replacement-table – stored in a single text file — within a hash file (for fast lookups), or even served through an external program!
For better performance I’d generate a single text file using PHP, which contains the following:
 NetTools       3  Â
 NetTools/Messengers 34  Â
 .  Â
 .  Â
 .  Â
 and so on.
The httpd.conf
file would contain:
 RewriteMap categories txt:/path/to/file/categoryids.txt  Â
 RewriteRule ^(.*)$ open.php?categoryid=${categories:$1|0}
These lines tell mod_rewrite to read the categoryids.txt file upon Apache startup, and provide the ID for the URL for open.php. The |0
means that categoryid
will be 0 if there’s no matching key in the textfile.
You can also choose to serve the IDs on-the-fly via a script or other executable code. The program is started by Apache on server startup, and runs until shutdown. The program must have buffered I/O disabled, read from the stdin, and write results to stdout — it’s that simple!
With RewriteMap you can do a lot more, including:
- load balancing through servers (using
rnd:
), - creation of a Webcluster that has an homogenous URL layout,
- redirection to mirror sites without modifying your Web application,
- denial of user access based on a hostlist,
and so on.
Tips, Tricks and Advice
- Before using mod_rewrite in a production server, I’d recommend setting up a testserver (or playground, whatever you prefer to call it).
E_ALL
); everywhere (and I recommend it!), but I find it boring to do the following for the ten thousandths time: if (isset($_GET['id']) && (validNumber($_GET['id'])) Â Â
if (isset($_GET['todo']) && ($_GET['todo']=='deleteitem'))
The following trick helped me to get rid of the extra isset()
expression by providing all the needed parameters each time in the RewriteRules
:
RewriteRule ^/products/[0-9]+$ products.php?id=$1&todo=
I know, I know it’s not the answer to the meaning of life — but it’s hard to show how nice and clear a solution this might provide in such a short example.
Finally …
That’s all for our ‘brief’ overview of mod_rewrite. After you’ve mastered the basics, you’ll find you can easily create your own rules. If you like the idea of URL rewriting, may want to play with mod_rewrite – some ideas follow (note that the underlying PHP code is not important in this case):
http://www.mysite.com/1/2/3/content.html  Â
  => 1_2_3_content.html  Â
  http://www.mysite.com/1/2/3/content.html  Â
  => content.php ? category=1  Â
 Â
  http://www.mysite.com/1/2/3/  Â
  => content.php ? category=1 & subcat1 = 2 & subcat2 = 3  Â
 Â
  http://www.mysite.com/1/2/3/details  Â
  => content.php ? category=1 & subcat1 = 2 & subcat2 = 3  Â
  http://www.mysite.com/bookshop/browse/bytitle  Â
  => library.php ? target=listbooks & order = title  Â
  http://www.mysite.com/bookshop/browse/byauthor  Â
  => library.php ? target=listbooks & order = author  Â
 Â
http://www.mysite.com/bookshop/product/123 Â Â
  => library.php ? target=showproduct & itemid=123  Â
 Â
http://www.mysite.com/bookshop/helpdesk/2 Â Â
  => library.php ? target=showhelp & page=2  Â
 http://www.mysite.com/bookshop/registration  Â
  => library.php ? target=reg
Links
- RegExp FAQs, Help and tutorials
- Search-engine friendly URLs(fortunately, the URLs listed here are already search-engine friendly :)
If you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like PHP & MySQL Web Development for Beginners.