Friday, April 4, 2008

Using mod rewrite in Apache

In a previous post I discussed URL rewriting in .NET. I was recently asked for advice on rewriting URLs in an Apache environment. So below is my answer to that question.

In the .htaccess file we do the following:
RewriteEngine On
#if query sting exists, then append to var2
RewriteCond %{HTTP_HOST} ^myschool\.edu$ [NC] RewriteRule ^(.*)$$1
RewriteCond %{QUERY_STRING} ^(.*)
RewriteRule ^(.*)((.(htm|php))|\/)$ pages.php?var1=$1&%1 [L] RewriteRule ^$ home.htm [L]

(Entries preceded by # are commented lines of code.)
The first line, RewriteCond is checking to see if the URL is<restofdomain>. In that case we change it to<rest of domain>. The R states that this is a forced redirection (thus is updates the URL's browser to display instead of just 301 is the code, meaning permanent redirection. L means last command, stop executing. (When this occurs, it will restart the Rewrite condition upon load.)
The next rewriteCond command is to ensure any pre-existing Query Strings are passed on.
The next Rewrite rule, checks to see if the page includes a .htm, .php, or a forward slash (/).
If so we rewrite the URL as described. The [L] signifies that last command.
Lastly, if it's not that, then that means it's just in which case we modify it to be

Basically we are rewriting the URL behind the scenes to the appropriate page. Here is an example is rewritten to
From there, our pages.php code retrieves the appropriate page from our CMS (home-grown in our case) and builds the page based on its settings. (Currently our pages are always built dynamically instead of dynamically.)
However, in the browser it still appears as the original URL.
For more information on mod_rewrite click here.