Web.config URL 重写 - 强制 www 前缀和 https [英] Web.config URL rewrite - force www prefix and https
问题描述
我正在尝试强制使用 https 和 www 前缀.但是我的规则并不完全有效.这是我的规则:
I'm trying to enforce https and a www prefix. However my rule doesn't fully work. Here is my rule:
<rewrite>
<rules>
<clear />
<rule name="Force https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.mydomain.co.uk/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Force www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
<add input="{HTTP_HOST}" pattern="www.mydomain.co.uk" negate="true" />
</conditions>
<action type="Redirect" url="https://www.mydomain.co.uk/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
- 它适用于将 http 重定向到 https.
- 如果我转到 https://mydomain.co.uk(重定向到 https://www.mydomain.co.uk)
- 但是,如果我去https://mydomain.co.uk/blah/,它不起作用随便
- It works for redirecting http to https.
- it works if I go to https://mydomain.co.uk (redirects to https://www.mydomain.co.uk)
- however it DOES NOT work if I go to https://mydomain.co.uk/blah/whatever
请问有人可以建议吗?谢谢.
Please can somebody advise? Thanks.
推荐答案
这些是我用于该确切目的的重写规则.我还添加了一条规则,使 URL 全部小写,并添加了一条规则来删除尾部斜杠(如果存在).这使得使用 Analytics 更容易,因为它将 page.aspx 和 page.aspx/视为不同的 url.这就是我使用 ignoreCase=true
的原因,因为如果有人在某处使用大写并不重要,因为稍后将通过 ToLowerCase
规则处理
These are the rewrite rules that I use for that exact purpose. I've also added a rule to make the URL all lowercase and a rule to remove the trailing slash should one be present. This makes working with Analytics easier since it treats page.aspx and page.aspx/ as different url's. That is why I use ignoreCase=true
because then it does not matter if someone uses upper case somewhere since it will be handled later on by the ToLowerCase
rule
<rule name="ForceWWW" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yoursite.com" />
</conditions>
<action type="Redirect" url="https://www.yoursite.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="HTTPtoHTTPS" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="RemoveTrailingSlash">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<rule name="ToLowerCase">
<match url=".*[A-Z].*" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
<conditions>
<add input="{URL}" pattern="WebResource.axd" negate="true" />
<add input="{URL}" pattern="ScriptResource.axd" negate="true" />
</conditions>
</rule>
这篇关于Web.config URL 重写 - 强制 www 前缀和 https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!