【问题标题】:301 redirection for wildcard urls in webconfig rulesWeb 配置规则中通配符 url 的 301 重定向
【发布时间】:2020-09-25 00:54:37
【问题描述】:
【问题讨论】:
标签:
c#
.net
redirect
iis
http-status-code-301
【解决方案1】:
您可以使用 {PATH_INFO} 代替 {HTTP_HOST}
<rule name="Redirect blog" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{PATH_INFO}" pattern="/blog$" />
</conditions>
<action type="Redirect" url="https://blog.example.com/" redirectType="Permanent"/>
</rule>
【解决方案2】:
如果你只想匹配那些以 blog 结尾的,那么你可以试试下面的规则:
<rule name="Redirect blog" stopProcessing="true">
<match url="^blog(/)?(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
</conditions>
<action type="Redirect" url="https://blog.example.com/{R:2}" />
</rule>
如果你想匹配所有,那么你可以试试这个规则:
<rule name="Redirect blog" stopProcessing="true">
<match url="^blog(/)?(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
</conditions>
<action type="Redirect" url="https://blog.example.com/" />
</rule>
【解决方案3】:
我找到了解决办法
<rule name="Blog Redirect" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?example.com$" />
<add input="{REQUEST_URI}" pattern="^/blog$" />
</conditions>
<action type="Redirect" url="https://blog.example.com" appendQueryString="false" redirectType="Permanent" />
</rule>