Brian Tanaka's Blog

[ Home | briantanaka.com | RSS 2.0 | 2010 Archive | 2009 Archive | 2008 Archive ]

Wed, 16 Jul 2008

Redirecting a GET URL with a Name-Value Pair to a Flat Directory URL

The problem -- You have an HTML form whose action points produces a GET URL like this: http://example.com/foo.php?s=bar+baz but you want to redirect it to a flat directory style URL like: http://example.com/bar+baz

The solution -- Use Apache's mod_rewrite. In your .htaccess file do this:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule .* /%1? [R,L]

How it works:

Hope that helps someone out there. The important thing to remember here is that you have to match the name-value pair in the QUERY-STRING condition, not in the RewriteRule. If you notice anything incorrect about this solution and want to comment, send me an email and I'll post your comment as an addendum.

posted at: 11:11 | path: /apache | permanent link to this entry

Made with PyBlosxom