The Definitive Guide to Apache Mod_Rewrite

Welcome to mod_rewrite, the Swiss Army Knife of URL manipulation! (apache docs)

I just stumbled upon a text entitled The Definitive Guide to Apache Mod_Rewrite. At first I was surprised that such a techincal topic would be discussed on dead trees, but moreover I was surprised people would pay to buy it, because everything you need to know about Mod_Rewrite is covered in the Apache doc. Yet after perusing the amazon wishlists of the web luminaries, including but not limited to Mr Neylon, I found this book of universal desirability. So what is so interesting about Mod_Rewrite?


It centres on making better public URLs, that is to say better URLs for visitors, you can keep you actual files in whatever messy fashion you desire, and with a set of logical rules still serve visitors the content they request.


Best practice
Before looking at the technalities lets consider the principles

  • URLs should be human-readable
  • URLs should be short
  • URLs should be hierarchical
  • URLs should be permanently unique

I prefer seeing examples so here is another way of expressing what I’ve just said

  • Good example: http://example.com/cars/bmw/e39/
  • Bad example: http://example.com/cgi-bin/coolscript.pl?session=12345678&loggedin=false&level1=cars&level2=bmw&level3=e39

Hey mom, look at my coolscript
The latter bad example looks so silly, but it is so common today. Lets look at whats wrong with it

  • cgi-bin: in the future you might figure out how to enable script processing in the root and move th script
  • coolscript.pl: if a vulnerability is discovered in the script hackers will hightail it to their favourite searchengine with the query “url:coolscript.pl” and find your site, also in the future you might change to another backend
  • ?session=12345678: use cookies instead, if you are relying on a URL then you are inviting brute force hackers
  • &loggedin=false: use cookies instead, if you are relying on a URL then you are inviting brute force hackers
  • &level1=cars&level2=bmw&level3=e39: this isn’t serch engine friendly

and of course, details aside, the URL is just too long


But fear not you can keep your script, just spend a few minutes adding Mod_Rewrite code to the .htaccess and you can have the good URL up and running.


Enough talk show me some examples that I can copy and paste
My thoughts exactly, I can’t understand the apache docs! It’s actually easy to put together basic rewrites however once things get complicated you get stuck. For example I need the code to perform the following


example.com/countriesireland.php (the actual file) > example.com/countries/ireland (the public URL)
and
example.com/countries.php (the actual file) > example.com/countries (the public URL)


Help required, apply within
As you can see I’ve made multiple php files in the root, and I want everything after the tld to be read, stripped of the forward slash, appended with .php, and returned to the requester.
Can anyone help? Please leave a comment with the code, or I’ll have to pull an all-nighter with a Snickers and Can of Coke.



3 Responses to “The Definitive Guide to Apache Mod_Rewrite”


  1. 1 Michele Sep 12th, 2006 at 6:59 pm

    I like reading things on dead trees :)
    Regarding your mod_rewrite problems I’d recommend posting here:
    http://www.irishwebmasterforum.com/server-technical-adminstration-tips-queries/

    someone can probably guide you …

    Personally I find the Apache docs can be helpful, but they’re not the most accessible

  2. 2 admin Sep 12th, 2006 at 7:59 pm

    Michele, thanks for the URL, I’ll search the archive there.

    BTW I see you have

    isquattedyour.ie

    , cool domain, but how did you manage to get it using the

    Natural Person > Discretionary Name

    route?

  3. 3 Michele Sep 12th, 2006 at 8:54 pm

    I have the full set :) isquattedyour.ie / .eu and .com

    It’s:
    descr: Natural Person
    descr: Discretionary Name

Leave a Reply




Advertising