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.
Continue reading ‘The Definitive Guide to Apache Mod_Rewrite’

Comments