r3plica
r3plica

Reputation: 13397

web.config redirect to www and redirect old pages to subdomain

I hope you can help me. Originally I set up a rule to redirect all non-www traffic to the www site like this:

<rewrite>
    <rules>
        <rule name="Redirect https://example.com to https://www.example.com HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*"></match>
            <conditions>
                <add input="{HTTP_HOST}" pattern="^example.com$"></add>
                <add input="{HTTPS}" pattern="on"></add>
            </conditions>
            <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
        </rule>
    </rules>
</rewrite>

Later on down the line we created a new site and moved the old site to https://business.example.com

When this happened, I was asked to create 301 redirects for the old blog posts to the new sub domain. I created these rules:

<rewrite>
    <rules>
        <rule name="301 Redirect 1" stopProcessing="true">
            <match url="live-better" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.example.com$" />
            </conditions>
            <action type="Redirect" url="https://business.example.com/live-better" redirectType="Permanent" />
        </rule>
        <rule name="301 Redirect 2" stopProcessing="true">
            <match url="a-zero-sum-game-in-retail" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.example.com$" />
            </conditions>
            <action type="Redirect" url="https://business.example.com/live-better/a-zero-sum-game-in-retail" redirectType="Permanent" />
        </rule>
        <rule name="301 Redirect 3" stopProcessing="true">
            <match url="buying-online-doesnt-mean-shopping-online" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.example.com$" />
            </conditions>
            <action type="Redirect" url="https://business.example.com/live-better/buying-online-doesnt-mean-shopping-online" redirectType="Permanent" />
        </rule>
        <rule name="301 Redirect 4" stopProcessing="true">
            <match url="the-truth-about-experiential-retail" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.example.com$" />
            </conditions>
            <action type="Redirect" url="https://business.example.com/live-better/the-truth-about-experiential-retail" redirectType="Permanent" />
        </rule>
        <rule name="301 Redirect 5" stopProcessing="true">
            <match url="winning-the-battle-for-brand-engagement" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.example.com$" />
            </conditions>
            <action type="Redirect" url="https://business.example.com/live-better/winning-the-battle-for-brand-engagement" redirectType="Permanent" />
        </rule>
        <rule name="301 Redirect 6" stopProcessing="true">
            <match url="experiential-technology-in-retail" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.example.com$" />
            </conditions>
            <action type="Redirect" url="https://business.example.com/live-better/experiential-technology-in-retail" redirectType="Permanent" />
        </rule>
        <rule name="301 Redirect 7" stopProcessing="true">
            <match url="what-retailers-have-forgotten" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.example.com$" />
            </conditions>
            <action type="Redirect" url="https://business.example.com/live-better/what-retailers-have-forgotten" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>

When I initally tested this, it seemed to be working. But upon further investigation only the live-better path is redirected properly.

I changed my 301 redirects to this:

<rule name="301 redirects" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="ON" />
        <add input="{PATH_INFO}" pattern="^/live-better" />
        <add input="{PATH_INFO}" pattern="^/live-better/a-zero-sum-game-in-retail" />
        <add input="{PATH_INFO}" pattern="^/live-better/buying-online-doesnt-mean-shopping-online" />
        <add input="{PATH_INFO}" pattern="^/live-better/the-truth-about-experiential-retail" />
        <add input="{PATH_INFO}" pattern="^/live-better/winning-the-battle-for-brand-engagement" />
        <add input="{PATH_INFO}" pattern="^/live-better/experiential-technology-in-retail" />
        <add input="{PATH_INFO}" pattern="^/live-better/what-retailers-have-forgotten" />
    </conditions>
    <action type="Redirect" url="https://business.example.com/{R:1}" redirectType="Permanent" />
</rule>

But it seems that now everything is redirected to https://business.example.com. I thought it might be the original rule:

<rule name="Redirect https://example.com to https://www.example.com HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
    <match url=".*"></match>
    <conditions>
        <add input="{HTTP_HOST}" pattern="^example.com$"></add>
        <add input="{HTTPS}" pattern="on"></add>
    </conditions>
    <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
</rule>

If I am not mistaken, it will try to redirect https://anything.example.com to https://www.example.com (which might be why the redirects are not working).

Can anyone with more experience with this tell me what I am doing wrong? I need the redirect from non-www to www, but at the same time I need the 301 redirects for the old blog posts.

Any help is massively appreciated.

Upvotes: 0

Views: 280

Answers (1)

r3plica
r3plica

Reputation: 13397

I think I solved it. It appears that order matters, so I changed the order so that "live-better" was last. I updated the non-www to www by adding the scheme like this:

<rule name="Redirect to www" stopProcessing="true">
    <match url="(.*)" />
    <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^situlive.com$" />
    </conditions>
    <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.situlive.com/{R:1}" />
</rule>

(thanks to this post: web.config redirect non-www to www)

so the full section now looks like this:

    <rewrite>
        <rules>
            <rule name="Redirect to www" stopProcessing="true">
                <match url="(.*)" />
                <conditions trackAllCaptures="false">
                    <add input="{HTTP_HOST}" pattern="^example.com$" />
                </conditions>
                <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" />
            </rule>
            <rule name="301 Redirect 1" stopProcessing="true">
                <match url="a-zero-sum-game-in-retail" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www.example.com$" />
                </conditions>
                <action type="Redirect" url="https://business.example.com/live-better/a-zero-sum-game-in-retail" redirectType="Permanent" />
            </rule>
            <rule name="301 Redirect 2" stopProcessing="true">
                <match url="buying-online-doesnt-mean-shopping-online" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www.example.com$" />
                </conditions>
                <action type="Redirect" url="https://business.example.com/live-better/buying-online-doesnt-mean-shopping-online" redirectType="Permanent" />
            </rule>
            <rule name="301 Redirect 3" stopProcessing="true">
                <match url="the-truth-about-experiential-retail" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www.example.com$" />
                </conditions>
                <action type="Redirect" url="https://business.example.com/live-better/the-truth-about-experiential-retail" redirectType="Permanent" />
            </rule>
            <rule name="301 Redirect 4" stopProcessing="true">
                <match url="winning-the-battle-for-brand-engagement" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www.example.com$" />
                </conditions>
                <action type="Redirect" url="https://business.example.com/live-better/winning-the-battle-for-brand-engagement" redirectType="Permanent" />
            </rule>
            <rule name="301 Redirect 5" stopProcessing="true">
                <match url="experiential-technology-in-retail" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www.example.com$" />
                </conditions>
                <action type="Redirect" url="https://business.example.com/live-better/experiential-technology-in-retail" redirectType="Permanent" />
            </rule>
            <rule name="301 Redirect 6" stopProcessing="true">
                <match url="what-retailers-have-forgotten" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www.example.com$" />
                </conditions>
                <action type="Redirect" url="https://business.example.com/live-better/what-retailers-have-forgotten" redirectType="Permanent" />
            </rule>
            <rule name="301 Redirect 7" stopProcessing="true">
                <match url="live-better" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www.example.com$" />
                </conditions>
                <action type="Redirect" url="https://business.example.com/live-better" redirectType="Permanent" />
            </rule>
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
                <match url="^main.js\/debug[\/]?" />
            </rule>
            <rule name="StaticContent">
                <action type="Rewrite" url="public{REQUEST_URI}"/>
            </rule>
            <rule name="DynamicContent">
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                </conditions>
                <action type="Rewrite" url="main.js"/>
            </rule>
            <rule name="Angular Routes" stopProcessing="true">
                <match url=".*" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="index.html" />
            </rule>
        </rules>
        <rewriteMaps>
            <rewriteMap name="MapProtocol">
                <add key="on" value="https" />
                <add key="off" value="http" />
            </rewriteMap>
        </rewriteMaps>
    </rewrite>

Upvotes: 0

Related Questions