IIS 将非 WWW 重定向到 WWW 以及将 HTTP 重定向到 HTTPS 问题
IIS Redirecting non-WWW to WWW along with HTTP to HTTPS ISSUE
我想在 IIS 8.5 中将非 WWW 重定向到 WWW,同时将 HTTP 重定向到 HTTPS。如果用户输入 http://example.com, http://www.example.com, or https://example.com, they all redirect to https://www.example.com.
这是我的 web.config 文件
<rewrite>
<rules>
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite
请注意,在 IIS 中我只有 1 个绑定 example.com,问题是:对于它重定向到 https://www.example.com BUT the start page of IIS appears not my Default.aspx, and When i type https://www.example.com/Default.aspx 的所有 URL,它给出 404 错误。
您需要将每个主机名绑定到一个网站。任何未明确绑定到网站(或服务器 IP 地址)的主机名将由 IIS 中的默认网站处理(绑定 *
- 意味着任何未绑定到 运行 网站的主机名)。
一个很好的简单设置是在 IIS 中创建 2 个网站(我们称它们为 Application
和 Rewrites
)- 一个用于托管您的应用程序,第二个用于处理将其他域重写到您的主域.您绑定到 Rewrite
网站的任何域都会将其流量重定向到 https://www.example.com.
###Application
网站
- 仅在端口 443(仅限 SSL)上绑定您的应用程序主机名
www.example.com
- 将您的应用程序部署到此 webroot
###Rewrite
网站
- 在 IIS 中创建一个新网站,并为其 webroot 创建一个新文件夹。
- 在端口 443 和端口 80 上绑定
example.com
。同时仅在端口 80 上绑定 www.example.com
。
在Rewrite
网站webroot文件夹中创建一个web.config如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect any traffic to Primary hostname" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
任何访问 http://example.com、https://example.com 或 http://www.example.com 将重定向到 https://www.example.com(包括任何 path/query 字符串)
注意:您可以使用默认网站作为 Rewrite
网站,或将通配符 *
绑定从默认网站移动到新的 Rewrite
网站 - 以重定向任何域给你的 https://www.example.com - 然而这是不好的做法/不建议。
先决条件:要使用此解决方案,您需要安装 IIS URL Rewrite extension(如果您尚未安装)。
我想在 IIS 8.5 中将非 WWW 重定向到 WWW,同时将 HTTP 重定向到 HTTPS。如果用户输入 http://example.com, http://www.example.com, or https://example.com, they all redirect to https://www.example.com.
这是我的 web.config 文件
<rewrite>
<rules>
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite
请注意,在 IIS 中我只有 1 个绑定 example.com,问题是:对于它重定向到 https://www.example.com BUT the start page of IIS appears not my Default.aspx, and When i type https://www.example.com/Default.aspx 的所有 URL,它给出 404 错误。
您需要将每个主机名绑定到一个网站。任何未明确绑定到网站(或服务器 IP 地址)的主机名将由 IIS 中的默认网站处理(绑定 *
- 意味着任何未绑定到 运行 网站的主机名)。
一个很好的简单设置是在 IIS 中创建 2 个网站(我们称它们为 Application
和 Rewrites
)- 一个用于托管您的应用程序,第二个用于处理将其他域重写到您的主域.您绑定到 Rewrite
网站的任何域都会将其流量重定向到 https://www.example.com.
###Application
网站
- 仅在端口 443(仅限 SSL)上绑定您的应用程序主机名
www.example.com
- 将您的应用程序部署到此 webroot
###Rewrite
网站
- 在 IIS 中创建一个新网站,并为其 webroot 创建一个新文件夹。
- 在端口 443 和端口 80 上绑定
example.com
。同时仅在端口 80 上绑定www.example.com
。
在Rewrite
网站webroot文件夹中创建一个web.config如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect any traffic to Primary hostname" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
任何访问 http://example.com、https://example.com 或 http://www.example.com 将重定向到 https://www.example.com(包括任何 path/query 字符串)
注意:您可以使用默认网站作为 Rewrite
网站,或将通配符 *
绑定从默认网站移动到新的 Rewrite
网站 - 以重定向任何域给你的 https://www.example.com - 然而这是不好的做法/不建议。
先决条件:要使用此解决方案,您需要安装 IIS URL Rewrite extension(如果您尚未安装)。