简体   繁体   English

web.config中的“非www到www”和“ https”重写规则,而不是localhost ASP.NET MVC

[英]“non-www to www” and “https” rewrite rule in web.config but not localhost ASP.NET MVC

I have the following rewrite rule in the web.config of my ASP.NET MVC 5 project: 我的ASP.NET MVC 5项目的web.config中具有以下重写规则:

<rule name="Redirect example.com to www.example.com and enforce https" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^[^www]" />
        <add input="{HTTPS}" pattern="off" />
    </conditions>
    <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

The rule redirects non-www to www and http to https (so something like http://example.com/hey will redirect to https://www.example.com/hey ) and works fine. 该规则将非www重定向到www,将http重定向到https(因此类似http://example.com/hey将重定向到https://www.example.com/hey )并且可以正常工作。 It however also works on localhost , and I can't seem to be able to work around it — I've tried negation rules and regular expressions containing | 但是,它也可以在localhost ,而且我似乎无法解决该问题-我尝试了包含|否定规则和正则表达式| but can't seem to be able to find the correct combinations. 但似乎无法找到正确的组合。 Am I approaching this the wrong way? 我是否以错误的方式处理此问题?

In conditions block you can use attribute negate="true" . conditions块中,可以使用属性negate="true" If this attribute is set and condition is matched, then rewrite rule is not applied. 如果设置了此属性且条件匹配,则不应用重写规则。

The description of negate attribute from IIS.net : IIS.netnegate属性的描述:

A pattern can be negated by using the negate attribute of the element. 可以通过使用元素的negate属性来否定模式。 When this attribute is used, the rule action is performed only if the current URL does not match the specified pattern. 使用此属性时,仅当当前URL与指定的模式不匹配时,才执行规则操作。

Since you are using MatchAny , adding additional attribute will not match, because at least one of the conditions will be met anyway. 由于您使用的是MatchAny ,因此添加附加属性将不匹配,因为无论如何至少要满足其中一个条件。 I recommend using 2 specific rewrite rules with logicalGrouping="MatchAll" , where each one is responsible just for single case: 我建议将2个特定的重写规则与logicalGrouping="MatchAll" ,其中每个规则仅负责一种情况:

<rule name="enforce https" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="off" />
        <add input="{HTTP_HOST}" matchType="Pattern" 
              pattern="^localhost(:\d+)?$" negate="true" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
          appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Redirect example.com to www.example.com"
       enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^[^www]" />
        <add input="{HTTP_HOST}" matchType="Pattern" 
              pattern="^localhost(:\d+)?$" negate="true" />
    </conditions>
    <action type="Redirect" url="https://www.example.com/{R:1}"
         appendQueryString="true" redirectType="Permanent" />
</rule>     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM