OCT 26
Code Tools

Ever wonder how your blog, with an address like http://www.yerblog/2010/10/26/Yer-Post-Title actually got that content? There isn’t an actual directory there – how does it work?

mod_rewrite rewrites a URL at the server level to load content different from what the URL is telling you. For example – a page could display the URL http://www.bloomfoundry.com/bands/accadacca – BUT IN REALITY the page is really loading from http://www.bloomfoundry.com/bands.php?asskickingrockers=accadacca. Pretty clever.

There are some things you need to do at the server level to enable it for your site. If you’re running a LAMP stack, you need the mod_rewrite module installed on your Apache server. If you’re using a hosting company, check with them (it was on for my rented space by default).

The actual code goes into the .htaccess file that’s at the root of your directory. A simple example would be something like:

RewriteEngine on
RewriteRule ^red.html$ green.html

With this, any time you load the URL red.html, you’d be loading CONTENT from green.html. Nobody would be any the wiser. If you wanted to let your users know that something was changing, you can insert a flag after the command to let them know chicanery’s happening under the surface. That would look like:

RewriteEngine on
RewriteRule ^red.html$ green.html [R]

With this, any time you load the URL red.html, you get redirected to green.html at the server level. It’s darn cool stuff, and you can do some really complex, interesting things with it. Here are some resources:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>