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都变为小写,并添加了一条规则,以删除尾随斜杠.由于它会将page.aspx和page.aspx/视为不同的URL,因此这使得使用Analytics(分析)更加容易.这就是为什么我使用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屋!