IIS将非WWW连同HTTP重定向到WWW到HTTPS ISSUE [英] IIS Redirecting non-WWW to WWW along with HTTP to HTTPS ISSUE
问题描述
我想在IIS 8.5中将非WWW连同HTTP重定向到WWW到HTTPS.如果用户键入 http://example.com ,则 http://www.example.com 或 https://example.com ,它们都重定向到 https://www.example.com .
I want to redirect non-WWW to WWW along with HTTP to HTTPS in IIS 8.5. if users type http://example.com, http://www.example.com, or https://example.com, they all redirect to https://www.example.com.
这是我的web.config文件
This is my web.config file
<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 ,但IIS的起始页不是我的Default.aspx,当我键入
Please note that in IIS i have ONLY 1 binding which is example.com, The problem is : for all the urls it redirects to 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 it gives 404 Error.
推荐答案
您将需要将每个主机名绑定到一个网站.未明确绑定到网站(或服务器IP地址)的任何主机名将由IIS中的默认网站处理(绑定 *
-表示未绑定到正在运行的网站的任何主机名).
You will need to bind each hostname to a website. Any hostnames not specifically bound to a website (or the servers IP address) will be handled by the Default Website in IIS (binding *
- meaning any hostname not bound to a running Website).
一个不错的简单设置是在IIS中创建2个网站(将其称为 Application
和 Rewrites
)-一个用于托管您的应用程序,第二个用于处理其他域的重写到您的主域.您绑定到 Rewrite
网站的任何域都会将其流量重定向到 https://www.example.com .
A nice easy setup is to create 2 Websites in IIS (lets call them Application
and Rewrites
) - one to host your application, a second to handle rewriting other domains to your primary domain. Any domain you bind to the Rewrite
Website will redirect its traffic to https://www.example.com.
- 仅在端口443上绑定您的应用程序主机名
www.example.com
(仅SSL) - 将您的应用程序部署到此webroot
- 在IIS中创建一个新网站,并为其webroot创建一个新文件夹.
- 在端口443和端口80上绑定
example.com
.也仅在端口80上绑定www.example.com
.
- Create a new Website in IIS, and create a new folder for its webroot.
- Bind
example.com
on port 443 and port 80. Also bindwww.example.com
on port 80 only.
在 Rewrite
网站webroot文件夹中创建一个web.config,如下所示:
Create a web.config in the Rewrite
Website webroot folder as follows:
<?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 , http://www.example.com 将重定向到 https://www.example.com (包括任何路径/查询字符串)
Any traffic accessing http://example.com, https://example.com, or http://www.example.com will be redirected to https://www.example.com (including any path/query string)
注意:您可以将默认网站用作 Rewrite
网站,或将通配符 *
绑定从默认网站移至新的 Rewrite
网站-将任何域重定向到您的 https://www.example.com -但这是一种不好的做法/不建议.
NOTE: You could use the default website as the Rewrite
website, or move the wildcard *
binding from the default website to a new Rewrite
website - to redirect any domain to your https://www.example.com - however this is bad practise / not advised.
这篇关于IIS将非WWW连同HTTP重定向到WWW到HTTPS ISSUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!