【问题标题】:web.config redirect to www and redirect old pages to subdomainweb.config 重定向到 www 并将旧页面重定向到子域
【发布时间】:2021-03-02 17:57:46
【问题描述】:

我希望你能帮助我。 最初我设置了一条规则,将所有非 www 流量重定向到 www 站点,如下所示:

<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>

后来我们创建了一个新站点并将旧站点移至https://business.example.com

发生这种情况时,我被要求为旧博客帖子创建 301 重定向到新的子域。 我创建了这些规则:

<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>

当我最初测试这个时,它似乎工作正常。但经过进一步调查,只有 live-better 路径被正确重定向。

我将 301 重定向更改为:

<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>

但现在似乎所有内容都重定向到https://business.example.com。 我认为这可能是原始规则:

<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>

如果我没记错的话,它会尝试将https://anything.example.com 重定向到https://www.example.com(这可能是重定向不起作用的原因)。

谁能告诉我我做错了什么? 我需要从非 www 重定向到 www,但同时我需要旧博客帖子的 301 重定向。

非常感谢任何帮助。

【问题讨论】:

    标签: iis url-rewriting


    【解决方案1】:

    我想我解决了。 看来顺序很重要,所以我更改了顺序,以便最后一个“生活得更好”。我通过添加这样的方案将非 www 更新为 www:

    <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>
    

    (感谢这篇文章:web.config redirect non-www to www)

    所以整个部分现在看起来像这样:

        <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 2013-06-26
      • 2014-03-25
      • 2018-10-09
      • 1970-01-01
      • 2013-09-18
      • 2022-03-04
      相关资源
      最近更新 更多