Problem Statement:
In order to improve the load time of my home page, I wanted to show a static HTML page for non-authenticated requests. However, WordPress handles search through the main entry point (index.php). I came up with this conditional Rewrite to handle any request with the ?s=search+term through index.php but let all other requests pass through to my static page.
Dependencies:
mod_rewrite and AllowOverrides enabled
The Rewrite condition matches any query string where the parameter name is `s` and followed by any query value. If the test is true, meaning the condition has been met, the first rewrite rule will be applied. Since the rule has the [L], or LAST flag, no other rules will be applied so the request will be handled by index.php. If the condition is not met, then the second rule will be applied and the request will be satisfied by the static.html page. This rule also has the LAST flag so no other rules will be applied.
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^$ index.php [L]
RewriteRule ^$ static.html [L]
This same pattern could be expanded and used to serve different pages, not just static pages, based on the query parameter name or even value. When this might come in handy is if you are running banner ads or an email campaign and use a query string to identify the source of the visit. You can then serve a customized experience to visitors based on the referrer.
RewriteCond %{QUERY_STRING} ^ref=origin-one$
RewriteRule ^$ home-page-one.html [L]
RewriteCond %{QUERY_STRING} ^ref=origin-two$
RewriteRule ^$ home-page-two.html [L]
RewriteCond %{QUERY_STRING} ^ref=origin-three$
RewriteRule ^$ home-page-three.html [L]