Mike Griffiths

Tag: mod rewrite

Apache Mod ReWrite

by Mike on May.02, 2009, under Web Development

I’ve noticed a few dodgey and miss-leading tutorials floating around recently for using mod rewrite.  Most people who search for mod rewrite tutorials just want to know how to make their URLs more SEO-friendly, for example, instead of /about.php?page=company&subsection=mission_statement, the URL would preferrably be /about/company/mission_statement – it’s easier to read from a human perspective and search engines love it.  

Below is a very brief tutorial on how to use Apache’s mod rewrite functionality.

First of all, make sure mod_rewrite is on.  In your httpd.conf (or whatever yours is called), make sure the following line is not hashed / commented out:

Loadmodule rewrite_module modules/mod_rewrite.so

You also need to make sure that ‘mod_rewrite.so’ exists in your modules directory, but I see no reason why it wouldn’t.  If you had to make any changes make sure you restart apache (apachectl -k restart / other variations).

Now we know mod_rewrite is activated we need to create a .htaccess file for the site we’re working on.  In the root directory of the website (/var/www/vhosts/example.com/httpdocs/) create a file called .htaccess – this is a hidden file that allows you to set directory-specific options for Apache.  In this file we need the following line at the top of the file:

Rewrite Engine on

This will turn the rewrite engine itself on. Now we can begin writing our rules.

RewriteRule ^about/company/mission_statement$ /about.php?page=company&subsection=mission_statement [L]
RewriteRule ^about/company$ /about.php?page=company [L]
RewriteRule ^about$ /about.php [L]

Let’s break this down. There are three lines, each is a new rule. The first rule checks for the URL /about/company/mission_statement, and if it finds the page the rule will call the PHP page: about.php with the GET vars page=company and subsection=mission_statement. If this rule isn’t matched it will move onto the next one, and so on. Everything between the ^ symbol and the dollar sign ($) is what is matched, you can use regexp here.

But consider this, you have 50+ pages in your about section, which are all pulled from a database. You’d have to sit there and add a new line to the .htaccess file for every page, not to mention every time you added a page you’d have to go in and manually edit the file. This is where wildcards come in, and can be very handy. Take the following example:

RewriteRule ^about/(.*)/(.*)$ /about.php?page=$1&subsection=$2 [L]
RewriteRule ^about/(.*)$ /about.php?page=$1 [L]
RewriteRule ^about$ /about.php [L]

That will match exactly the same as the first set of rules, the only difference is that the page and subsection variables are automatically populated with whatever lies in the URL. Obviously this means that extra caution is needed to stop SQL injections, but nothing you shouldn’t already be doing anyway.

I hope this was useful for someone.

Leave a Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post.