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 ^(.*)$ http://www.myschool.edu/$1
RewriteCond %{QUERY_STRING} ^(.*)
RewriteRule ^(.*)((.(htm|php))|\/)$ pages.php?var1=$1&%1 [L] RewriteRule ^$ home.htm [L]
[R=301,L]

(Entries preceded by # are commented lines of code.)
The first line, RewriteCond is checking to see if the URL is http://myschool.edu/<restofdomain>. In that case we change it to http://www.myschool.edu/<rest of domain>. The R states that this is a forced redirection (thus is updates the URL's browser to display www.myschool.edu instead of just myschool.edu. 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 www.myschool.edu in which case we modify it to be www.myschool.edu/home.htm

Basically we are rewriting the URL behind the scenes to the appropriate page. Here is an example
http://www.myschool.edu/Current-Students.htm is rewritten to http://www.myschool.edu/pages.php?var1=Current-Students
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.