【问题标题】:How to dynamically rewrite/redirect url如何动态重写/重定向 url
【发布时间】:2019-11-15 19:22:13
【问题描述】:

我在这里遇到了一个复杂的问题。我搜索了其他主题,但没有找到解决方案。 我有一个网站 URL,比如 foo.com,还有一个名为 bar.com 的 URL。他们共享相同的代码。现在一切都很好,除了重定向 URL 部分。我也想让你们知道我没有 URL 重写的经验,所以请保持简单。

Foo.com 首先存在,然后通过 CanonicalHostNameRule 从 foo.com 重定向到 www.foo.com,将模式 (.*) 重定向到 http://www.foo.com/{R1},这对于该域非常有用,但不适用于 bar.com 域.

以下是我对网站的完整重写规则:

这些是我的 web.config 重写规则:

<rules>
                <clear />
                <rule name="LetsEncrypt Rule" stopProcessing="true">
                    <match url="^\.well-known.*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="None" />
                </rule>
                <rule name="CanonicalHostNameRule1" enabled="false" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^www.\foo.be$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.{R:2}/{R:1}" />
                </rule>
                <rule name="Rewrite legacy url's" patternSyntax="ECMAScript">
                    <match url="^.((?!nl-BE).)+$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
                    </conditions>
                    <action type="Redirect" url="{C:1}" redirectType="Permanent" />
                </rule>
                <rule name="One homepage - redirect /home/">
                    <match url="^([a-z][a-z]-[A-Z][A-Z])/home/?" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="{R:1}/" redirectType="Permanent" />
                </rule>
                <rule name="One homepage - redirect root to language">
                    <match url="^\d*$" negate="false" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="/nl-BE/" redirectType="Permanent" />
                </rule>
                <rule name="Add trailing slash">
                    <match url="^([^.]*[^/])$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="{R:1}/" redirectType="Permanent" />
                </rule>
                <rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                </rule>
            </rules>

你们能否让我了解如何解决这个问题?这对我的小脑袋来说太复杂了。提前谢谢各位!

【问题讨论】:

  • 您在哪里添加此规则?在站点级别还是服务器级别?你能分享你的 web.config 文件 url 重写规则代码吗?并且两者都是同一站点或不同站点的主机名?
  • 感谢@JalpaPanchal 的回复,我用 web.config 中的重写规则更新了我的问题。

标签: url redirect iis url-rewriting host


【解决方案1】:

你可以试试下面的url重写规则:

 <rule name="Canonical3" enabled="true" stopProcessing="true">
         <match url=".*" />
         <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^([a-z0-9]+[.]com)$" />
         </conditions>
         <action type="Redirect" url="http://www.{C:1}/{R:0}" redirectType="Permanent" />
       </rule>

这将适用于两个域名。

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多