Main Content

Passing GET parameters through Apache mod_rewrite

Archive - Originally posted on "The Horse's Mouth" - 2006-12-27 13:05:21 - Graham Ellis

If you're using Apache mod_rewrite to redirect a series of URLs to a single script, did you know that you can pass values entered onto a form via the GET method through as well? Simply add on %{QUERY_STRING} onto the end of your new URL. You'll need to add a ? into the target URL to ensure that the query string is taken as such .... and you can even add more parameters if you wish.

Here's how we divert all requests to .htm, .html, .php, .php4 and .php5 files in a directory to a single script called b.php; we pass in any parameters the user filled in on his web page, and we generate an extra request parameter called pagename that contains ... of course ... the name of the page requested.

RewriteEngine On
RewriteRule ^(.*)\.htm b.php?pagename=&%{QUERY_STRING}
RewriteRule ^(.*)\.php b.php?pagename=&%{QUERY_STRING}