简体   繁体   中英

Extracting arguments in IIS rewrite - where am I going wrong?

How can I redirect '/path?foo=abc&bar=def' to '/path/abc/def'?

I've tried the following:

<rule name="My rule" stopProcessing="false">
    <match url="^path" />
    <conditions>
        <add input="{QUERY_STRING}" pattern="foo=/[^&amp;]*/" />
        <add input="{QUERY_STRING}" pattern="bar=(.*)" />
    </conditions>
    <action type="Redirect" url="/path/{C:0}/{C:1}" appendQueryString="false" />
</rule>

But this doesn't even process my match. Where am I going wrong?

I don't have access to a computer to verify this, but I think the following could simplify:

<rule name="Redirect old FAQ view" stopProcessing="false">
    <match url="^path.*" />
    <conditions>
        <add input="{QUERY_STRING}" pattern="^foo=([^&]*).*?bar=(.*)" />
    </conditions>
    <action type="Redirect" url="/path/{C:0}/{C:1}" appendQueryString="false" />
</rule>

You don't need the second input line as you can use multiple capturing groups in one. I'm not sure if the whole line will come back as a match or not so you may need to adjust to {C:1}/{C:2} . Finally, I'm not sure if the match url was problematic. It would contain the query string as well, so that may have been causing it to miss. Look here for more details on the various components of the URL rewrite process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM