Wednesday, May 23, 2007

Apache rewrite recipe for usable URLs

Here's a little mod_rewrite recipe for Apache that might be useful if you're developing a web app like StripMe which is a very simple service that takes a CSS file and returns a version of the file with all the comments and white space stripped out.

The location of the CSS file to be stripped is passed to the service in the query string of the URL. Normally, this URL would look something like this:
We wanted to make the service easy to use by letting users pass the URL of their CSS files like this:
To do this, I ended up using the following rewrites in Apache:
  • RewriteCond %{REQUEST_URI} "http:\/\/"
  • RewriteRule "(.*)http:\/\/(.*)" "$1index.php?http:\/\/$2" [PT]
It's probably not the most elegant solution, but as long as there's a script in your root directory called index.php that knows what parameters to expect, it should work.

Please let me know if it doesn't work or if there's a nicer way of doing it.

No comments: