Forum Moderators: phranque

Message Too Old, No Replies

One RewriteCond for multiple RewriteRules

Preserve %{QUERY_STRING} in all rewrites

         

anjanesh

7:10 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



I need to preserve all user query strings.

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^$ index.html?page=home&%1

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^about$ index.html?page=about&%1

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^contact$ index.html?page=contact&%1

How can I specify the RewriteCond for all RewriteRules ?

I am not looking forward to single all-purpose controller RewriteRule since this is a small static website.

jdMorgan

5:28 am on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need a RewriteCond for this. Use the [QSA] flag (Query String Append) on RewriteRule instead.

See the Apache mod_rewrite documentation for more info.

These two lines of code replace your six:

RewriteRule ^$ index.html?page=home [QSA,L]
#
RewriteRule ^(about|contact)$ index.html?page=$1 [QSA,L]

It is unusual to rewrite to a .html file with query parameters. Unless you are parsing .html files for PHP includes, you may need to further modify your rules: Do not, for example, rewrite these extensionless URL requests to .html filepaths, and then rewrite those .html filepaths to .php filepaths. Instead, this should be done all in one step, rewriting extensionless URLs direct to .php files.

Jim