Question

I have more than one rewrite map in my site, but only one map seems to be working.

The relevant chunk of the Web.config file reads:

<system.webServer>
    <rewrite>
        <rewriteMaps configSource="rewriteMaps.config"/>
        <rules>
            <rule name="Locale redirects">
                <match url="^/?[a-z][a-z](-[a-z][a-z])?/?$"/>
                <conditions>
                    <add input="{LocaleRedirects:{REQUEST_URI}}" pattern="(.+)"/>
                </conditions>
                <action type="Redirect" url="{C:1}" appendQueryString="true"/>
            </rule>
            <rule name="Short URL redirects">
                <match url="^/?[a-z][a-z](-[a-z][a-z])?/?$"/>
                <conditions>
                    <add input="{ShortURLs:{REQUEST_URI}}" pattern="(.+)"/>
                </conditions>
                <action type="Redirect" url="{C:1}" appendQueryString="true"/>
            </rule>
        </rules>
    </rewrite>
</system.webServer>

My file rewriteMaps.config (with several lines removed for demonstration purposes) reads:

<rewriteMaps>
    <rewriteMap name="ShortURLs">
        <add key="getstarted" value="/en-us/get-started/"/>
        <add key="support" value="/en-us/support/"/>
        <add key="terms" value="/en-us/terms-and-conditions/"/>

        <add key="getstarted/" value="/en-us/get-started/"/>
        <add key="support/" value="/en-us/support/"/>
        <add key="terms/" value="/en-us/terms-and-conditions/"/>
    </rewriteMap>
    <rewriteMap name="LocaleRedirects">
        <add key="/jp" value="/ja-JP/"/>
        <add key="/jp/" value="/ja-JP/"/>
        <add key="/kr" value="/ko-kr/"/>
        <add key="/kr/" value="/ko-kr/"/>
    </rewriteMap>
</rewriteMaps>

All the locale redirects are working perfectly; none of the short URLs is working, however; they just give a 404 response.

Is there something I'm doing obviously-wrong? Or is there some intricacy to using multiple rewrite maps that I'm missing somehow?

Was it helpful?

Solution

There are few issues here with your Rewrite Rules:

1) For Short URL redirects you are matching string such as /en-gb or /en-gb/ That is why your local redirects are working but not when string is "getstarted"

<rule name="Short URL redirects">
     <match url="^/?[a-z][a-z](-[a-z][a-z])?/?$"/>

Should be <match url="(.*)" or if you prefer reg ex as url="^/.*/?" this will match /getstarted or /getstarted/.

2) In your rewritemap you need to specify key as /getstarted instead of just getstarted.

<rewriteMap name="ShortURLs">
        <add key="getstarted" value="/en-us/get-started/"/>

Should be:

<rewriteMap name="ShortURLs">
        <add key="/getstarted" value="/en-us/get-started/"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top