Global Constant
Steve Nay's ramblings

Quick start guide to mod_rewrite

The Apache mod_rewrite module turns out to be a very useful tool when you’re building PHP applications. Here’s a simple quick start guide:

First, make sure Apache has the mod_rewrite module installed. In Ubuntu, you can execute this command:

Make sure that the AllowOverride directive is set to “All” for the folder containing your files. On Ubuntu, it’s under /etc/apache2/sites-enabled/, called 000-default or default-ssl.

Now, create an .htaccess file in your site’s root folder. Here’s an example:

Some notes:

I’ve set up a simple PHP script at driver-esl-handler.php like this:

Now if I go to a URL like this

I get the following response:

I found a great cheat sheet for mod_rewrite here. It’s worth perusing if you’re doing anything other than elementary rewriting. You can also read the official Apache docs.

Older article
Very short stories