[英]IIs 8 Redirect URL from http to https
我刚刚为我的dmain和主机设置了ssl。 我想将我的网站限制为仅限 https://www.example.com
。
如果任何用户试图打开http://example.com
, www.example.com
, example.com
或https://example.com
他必须被重定向到https://www.example.com
重定向必须仅适用于域名。 任何URL的其余部分将保持不变。
例如:如果用户打开example.com/dir1/page1.aspx
,则必须将其重定向到https://www.example.com/dir1/page1.aspx
我想使用IIS重写规则来做到这一点。
我通过将此代码添加到域的根目录上的web.config
文件来解决此问题。
www
开头的 http
或https
第二个规则匹配网址,如果它以www
开头但不是 https
<system.webServer> <rewrite> <rules> <rule name="Redirect from non www" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^example.com$" /> </conditions> <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" /> </rule> <rule name="Redirect from non https" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> <add input="{HTTP_HOST}" pattern="^www.example.com$" /> </conditions> <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer>
如果您无法控制iis或使用托管公司不允许您更改设置的共享主机,请在global.asax文件中添加此项。
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
{
Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);
}
}
编辑:
对于规范标记(www.example.com),您不需要对代码进行任何更改。 在你的plesk panel / odin面板中给出了一个设置(即使在共享主机中),为你的应用程序选择默认的URL。
它会自动将您的网站重定向到www.example.com
您网站托管设置下的设置。 选择www的首选域选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.