如何动态 rewrite/redirect url

How to dynamically rewrite/redirect url

我遇到了一个有点复杂的问题。我搜索了其他主题,但没有找到解决方案。 我有一个网站-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>

你们能告诉我如何解决这个问题吗?这对我的小脑袋来说太复杂了。提前谢谢大家!

您可以尝试以下 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>

这将适用于两个域名。