如何将所有非 www 的 URL 重定向到 https://www.在 IIS 中? [英] How to redirect all non-www URLs to https://www. in IIS?
问题描述
我想在 IIS 8.5 中添加正确的 301 永久重定向规则.我添加了以下规则,但它不起作用.
I want to add the proper 301 permanent redirect rule in IIS 8.5. I've added the following rules but it is not working.
<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".然后没有重定向.示例:https://www.example.com
- 如果 URL 是 HTTP,那么它应该被重定向到 HTTPS.示例:http://www.example.com 应该重定向到 https://www.example.com
- 如果 URL 使用 HTTPS 但不包含www".那么它应该被重定向到带有www"的HTTPS站点.字首.示例:https://example.com 应该重定向到 https://www.example.com
- 如果 URL 既不包含 HTTPS 也不包含 WWW,则使用www"重定向到 HTTPS URL.字首.示例:http://example.com 应该重定向到 https://www.example.com
- If the URL is in HTTPS and contains "www." then NO redirect. Example: https://www.example.com
- If the URL is in HTTP then it should be redirected to HTTPS. Example: http://www.example.com should be redirected to https://www.example.com
- If the URL is in HTTPS but does NOT contain "www." then it should be redirected to HTTPS site with "www." prefix. Example: https://example.com should be redirected to https://www.example.com
- If the URL neither contains HTTPS nor WWW then redirect to HTTPS URL with "www." prefix. Example: http://example.com should be redirected to https://www.example.com
总而言之,每个 URL 都应该是 HTTPS 并且应该有www".前缀.
注意:我已经安装了URL重写模块 在 IIS 中.
NOTE: I have installed URL Rewrite Module in IIS.
谁能帮我实现这个目标?
Can anyone please help me to achieve this?
推荐答案
我通过在 Web.config
文件中添加两个 URL 重写规则来管理它:
I managed it by adding two URL rewrite rules in Web.config
file:
- 用于将非 www 重定向到 https://www.{domain}.com/...
将 HTTP 重定向到 HTTPS
- For redirecting non-www to https://www.{domain}.com/...
Redirecting HTTP to HTTPS
<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 重写之外.如果不需要,您可以删除这些条件.
All the conditions with negate="true"
are used for exclusion. Hence all URLs which contains "localhost", "stage", and "dev" are excluded from URL rewrite. You can remove these conditions if not required.
Read more about negate attribute at http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
这篇关于如何将所有非 www 的 URL 重定向到 https://www.在 IIS 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!