我需要使用web.config中的规则将所有非www或非https流量重定向到https:// www
http://domain.com --> https://www.domain.com http://www.domain.com --> https://www.domain.com https://domain.com --> https://www.domain.com
我修改了Web.config. Redirect all traffic to www.my… Using rules element.的规则,但无法将http://www.domain.com重定向到https://www.domain.com.
<rewrite> <rules> <clear /> <rule name="Redirect to www subdomain"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" /> <add input="{SERVER_PROTOCOL}" pattern="^(.*)(/.*)?$"/> </conditions> <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/> </rule> </rules> </rewrite>
解决方法
这会将所有非www或非https重定向到
https://www
<rewrite> <rules> <rule name="non-www-https to www https" enabled="true" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" /> <add input="{HTTPS}" pattern="on" /> <add input="{HTTP_HOST}" pattern="localhost" negate="true" /> </conditions> <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" /> </rule> </rules> </rewrite>
我在IIS express(本地主机)上添加了这一行来忽略它:
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
这样做的另一个好处是您无需在此规则中输入您的域.
你可以在这里阅读更多:
http://weblogs.asp.net/owscott/redirecting-non-www-to-domain-equivalent