Rewrite maps in IIS7 — how to make the match optionally include a trailing slash?

后端 未结 2 1208
既然无缘
既然无缘 2021-02-07 12:51

I have read the top 30 Google hits for several combinations of IIS rewrite map condition and so on, but I can\'t find any decent documentation, either on a micros

相关标签:
2条回答
  • 2021-02-07 13:02

    Firstly, don't use dot, it matches everything and is naturally greedy. Use character negation instead: ([^\n]+). Try this, then re-run, and if that doesn't work, try adding /? again on the pattern attribute.

    0 讨论(0)
  • 2021-02-07 13:09

    This should do it:

    <rewrite>
        <rewriteMaps>
            <rewriteMap name="ShortURLs">
                <add key="terms" value="/en-us/terms-and-conditions/"/>
                <add key="privacy" value="/en-us/privacy-and-cookies/"/>
                <add key="buy" value="/en-us/where-to-buy/"/>
            </rewriteMap>
        </rewriteMaps>
        <rules>
            <rule name="Short URL redirects">
                <match url="^(.+?)/?$" />
                <conditions>
                    <add input="{ShortURLs:{R:1}}" pattern="(.+)" />
                </conditions>
                <action type="Redirect" url="{C:1}" appendQueryString="true"/>
            </rule>
        </rules>
    </rewrite>
    

    You were quite close; I only needed to make three small changes:

    • removed the leading slashes in the keys in the rewrite map
    • used the non-greedy quantifier +? in the rule's match
    • used a back reference to the match {R:1} in the condition input

    I share your experience in having trouble finding decent documentation; I had to experiment my way through, with help from the following articles:

    • http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module
    • http://technet.microsoft.com/en-us/library/ee215190(v=ws.10).aspx
    0 讨论(0)
提交回复
热议问题