我想在 IIS 8.5 中添加正确的 301 永久重定向规则。我添加了以下规则,但它不起作用。
<rule name="Redirect top domains with non-www to www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
<add input="{HTTP_HOST}" pattern="^(http:\/\/){0,1}(www\.){0,1}([^\.]+)\.([^\.]+)$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.{C:3}.{C:4}" redirectType="Permanent" />
</rule>
条件
总而言之,每个 URL 都应该是 HTTPS 并且应该有“www”。字首。
注意:我已安装 URL Rewrite Module在 IIS 中。
任何人都可以帮助我实现这一目标吗?
最佳答案
我通过在 Web.config
中添加两个 URL 重写规则来管理它文件:
<rule name="Redirect top domains with non-www to www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
<add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
<serverVariables>
<set name="Redirect" value="false" />
</serverVariables>
</rule>
<rule name="Force HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
negate="true"
的所有条件用于排除。因此,所有包含“localhost”、“stage”和“dev”的 URL 都被排除在 URL 重写之外。如果不需要,您可以删除这些条件。阅读更多关于 否定 属性在 http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
关于asp.net - 如何在 IIS 中将所有非 www URL 重定向到 https ://www.?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31406039/