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.
For your three examples, these will work:
The trick is the "QSA" flag.
edit: a slightly more-general solution, this based on how Drupal does it:
The !-f is important, because otherwise you couldn't serve images or index.html itself. The !-d line can be dropped, depending on what, exactly, you're doing. A slightly-different approach might be:
which would catch /foo and /bar, but not /foo/, /bar/, or /foo/bar.
Beside the fact that you can use QSA, are you aware that you can use
%{QUERY_STRING}
right within your RewriteRule?:The advantage of this method over the QSA flag is that you can control the order of parameters in the resulting querystring. E.g.:
This makes sure that the 'page' parameter is always set to 'contact', even if it is already contained in the original querystring. (At least this is true for PHP as the querystring parser of PHP always returns the last of multiple identical parameters into the $_GET array.)
One RewriteCond for multiple RewriteRules
Your headline implies another question that can be solved too:
You can use the RewriteRule flag
S|skip
to tie multiples RewriteRules to a single RewriteCond (or to a set of RewriteConds). Here is an example that applies one Cond to three Rules:But this is no solution to your original problem, as backreferences to the RewriteCond (like %1 in your case) will not work within the skipped rules.