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:
sudo a2enmod rewrite
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:
RewriteEngine On RewriteBase /~snay2/ RewriteRule ^driver-esl/(.*)/(.*)/$ driver-esl-handler.php?driver=$1&flowershop=$2 [QSA]
Some notes:
- The bolded sections are the capturing portions of the regex and where I use them in the rewritten URL.
- The [QSA] at the end of the line means "query string append": anything in the query string of the original request will get appended to the new query string Apache created on the fly.
- Change the RewriteBase to whatever the root URL of your site is. This was the biggest thing that tripped me up.
- For some reason, I had problems getting the rewriting to work on an EC2 Ubuntu instance when the URL and the PHP file had the same name. So I called the PHP file driver-esl-handler.php instead.
I’ve set up a simple PHP script at driver-esl-handler.php like this:
<?php header('Content-Type: text/plain'); print_r($_REQUEST); ?>
Now if I go to a URL like this
http://students.cs.byu.edu/~snay2/driver-esl/driver1/fs2/?_domain=rfq&_name=delivery_ready
I get the following response:
Array ( [driver] => driver1 [flowershop] => fs2 [_domain] => rfq [_name] => delivery_ready )
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.