[svlug] .htaccess rewrite rule

Brian J. Tarricone bjt23 at cornell.edu
Thu Mar 6 10:32:01 PST 2008


Ah, mod_rewrite...

Skip Evans wrote:
> 
> The user accesses the site with
> 
> http://prepcube.com/wi/madison/
> 
> I need a rewrite rule that will convert this to
> 
> http://precube.com/index.php?state=wi&school=madison
> 
> I know this should rewrite anything to index.php, 
> and this is what I have so far:
> 
> <IfModule mod_rewrite.c>
>          Options +FollowSymlinks
>          RewriteEngine On
>          RewriteRule ^(.*) index.php [L]
> </IfModule>

Something like this should work:

RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/(.*)$  /index.php?state=$1&school=$2 [L]

... though I'm not completely sure about that first match rule (it 
should be "match at least one character that is not a '/' char").  Give 
it a try and see what happens.  You can also add ",R" to the bracketed 
portion at the end of the rule while testing; your browser on the client 
side will get a hard redirect, and you can see what the redirect is 
evaluating to without digging through logs.

For future reference, this is a great reference with examples:
http://httpd.apache.org/docs/2.2/misc/rewriteguide.html
... linked right from the main apache 2.2 documentation pages. 
Searching for the actual mod_rewrite directive documentation is also 
useful to make sure you have your syntax right.

On a side note, assuming you've decided that your site *requires* 
mod_rewrite for proper operation, I'd suggest dropping the IfModule 
guards.  Personally I'd prefer to know that something is broken right 
when the web server starts, rather than getting confused emails from 
people about links not working.

	-brian



More information about the svlug mailing list