I'm sorry to be the one to bring you the bad news, but your rewrite rule isn't doing anything for you. The regular expression in the <match>
tag is invalid. First of all the query string is not part of the URL there and to match a ?
in a regular expression you have to use \?
as the ?
has special meaning in regular expressions.
And also the syntax in your rewritten url
is invalid. You can't use $1
for back reference but should use {R:x}
(for back references to the URL) or {C:x}
(for back references to the conditions) where x
is the number of the part of the regular expression you want to reference to.
Unfortunately it's not exactly clear to me what you are trying to accomplish. My guess is that you are trying to rewrite all requests that start with s=<something>
in the query string to /blog/?s=<something>
. If that's the case, then this rule should do that:
<rule name="Rewrite search queries" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="^s=([^&.]*)&?" />
</conditions>
<action type="Rewrite" url="blog/?s={C:1}" appendQueryString="false" />
</rule>
For simplicity, the s=<something>
needs to be the first parameter of the query string.
If you want to do something else, please edit your question and give a few more examples of URL's and how they will have to be rewritten.
Update: If you need the search form to use site.com/blog/?s=
instead of site.com/?s=
why not simply change the HTML of the search form? Just change the action
of that form.
To have the above rule work together with WordPress rewrite rules, you have to modify it slightly. First of all you have to remove stopProcessing="true"
and to be safe, modify the url
of the <action>
and add a leading slash. Might not be really necessary, just to be sure.
So the rewrite rule will become:
<rule name="Rewrite search queries">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="^s=([^&.]*)&?" />
</conditions>
<action type="Rewrite" url="/blog/?s={C:1}" appendQueryString="false" />
</rule>
And make sure this rule is above the WordPress rewrite rules. The WordPress rules should be the last rules.