url - 如何动态重写/重定向 url
问题描述
我在这里遇到了一个复杂的问题。我搜索了其他主题,但没有找到解决方案。我有一个网站 URL,比如说foo.com
,还有一个叫做bar.com
. 他们共享相同的代码。现在一切都很好,除了重定向 URL 部分。我也想让你们知道我没有 URL 重写的经验,所以请保持简单。
Foo.com 首先存在,并通过 CanonicalHostNameRule从foo.com
to重定向,将模式重定向到,这对于该域非常有效,但不适用于该域。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>
这将适用于两个域名。
推荐阅读
- spring - 为什么 Spring Security + Angular 登录在 AuthenticationSuccessHandler 和 RestController 上有不同的会话?
- vb.net - 检索文本文件中的特定数据
- android - 生成 Proguarded 签名的 APK 问题?
- python - 如何在 django 2.0 中找回密码?
- interface - 为 arty 板外部 DDR3 内存 (VHDL) 合成 MIG 7 系列时出现问题
- vba - 对所选内容旁边的新列中的列求和
- php - 无法让 php 代码工作。Echo 显示单词“echo”
- angular - 服务器上的 Angular 可观察问题
- terraform - 如何销毁 Terraform 托管的默认安全组中的规则?
- javascript - javascript未设置发布的数组元素