.NET MVC 2 RequireHttps - 如何将www.mysite.com重定向到mysite.com [英] .NET MVC 2 RequireHttps - How to redirect www.mysite.com to mysite.com
问题描述
在.NET MVC 2中,您可以应用< RequireHttps()>
属性来使方法受SSL保护。
In .NET MVC 2 you can apply the <RequireHttps()>
attribute to make a method be secured by SSL.
<RequireHttps()>
Function Index() As ActionResult
Return View()
End Function
假设您的SSL证书是针对mysite.com发布的。如果用户通过输入 http://www.mysite.com
,< RequireHttps()>
将它们重定向到 https://www.mysite.com
,这将使浏览器显示无效的证书警告。
Let's say that your SSL certificate is issued for mysite.com. If the user visits your site by entering http://www.mysite.com
, <RequireHttps()>
will redirect them to https://www.mysite.com
, which would make the browser display an invalid certificate warning.
切断www的最佳方法是什么?使用< RequireHttps()>
时的前缀?
What is the best way to chop off the www. prefix when using <RequireHttps()>
?
推荐答案
我在IIS 7上,可以访问URL Rewrite模块 http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/ 。
I'm on IIS 7 and have access to the URL Rewrite module http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/.
我解决了这个问题将它放在MVC 2应用程序的web.config文件中:
I solved this by putting this in the web.config file of the MVC 2 application:
<configuration>
...
<system.webServer>
<rewrite>
<rules>
<rule name="CanonicalHostNameRuleMain" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^mysite\.com$" negate="true" />
<add input="{HTTPS}" pattern="^off$" />
</conditions>
<action type="Redirect" url="https://mysite.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
http://mysite.com
- > https://mysite.com
http://www.mysite .com
- > https://mysite.com
https:// www .mysite.com
- > https://mysite.com
http://mysite.com
-> https://mysite.com
http://www.mysite.com
-> https://mysite.com
https://www.mysite.com
-> https://mysite.com
这是简单而且有效,但是只有一些优雅的MVC解决方案仍然会很好。
This is simple and it works, but it would still be nice to have some sort of elegant MVC only solution.
这篇关于.NET MVC 2 RequireHttps - 如何将www.mysite.com重定向到mysite.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!