C# 重定向到不带www的https,同时跳过localhost

C# 重定向到不带www的https,同时跳过localhost,c#,.net,azure,url-rewriting,web-config,C#,.net,Azure,Url Rewriting,Web Config,我正在尝试在Asp.Net项目中设置重写规则 重写到https并删除任何“www.”目前在生产环境中都可以使用,但当我尝试在本地主机上运行它时,它会不断将其重定向到https 这是一个运行在Azure上的Umbraco站点。这就是第一条规则存在的原因 这就是它目前的样子: 我有一条规则和两个条件试图跳过localhost的重定向,但它们都不起作用 <system.webServer> <rewrite> <rules> <rule n

我正在尝试在Asp.Net项目中设置重写规则

重写到https并删除任何
“www.”
目前在生产环境中都可以使用,但当我尝试在本地主机上运行它时,它会不断将其重定向到https

这是一个运行在Azure上的Umbraco站点。这就是第一条规则存在的原因

这就是它目前的样子: 我有一条规则和两个条件试图跳过localhost的重定向,但它们都不起作用

<system.webServer>    
<rewrite>
  <rules>
    <rule name="AlwaysOn agent requests without any redirections" stopProcessing="true">
      <match url="^$"/>
      <conditions>
        <add input="{HTTP_USER_AGENT}" pattern="^AlwaysOn$"/>
      </conditions>
      <action type="None"/>
    </rule>
    <rule name="Skip it all, if on localhost" stopProcessing="true">
      <match url="^localhost$"/>
      <conditions>
        <add input="{HTTP_HOST}" pattern="^localhost$"/>
      </conditions>
      <action type="None"/>
    </rule>
    <rule name="Remove www" stopProcessing="true">
      <match url="(.*)"></match>
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
        <add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    <rule name="Force HTTPS" enabled="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions>
        <add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost$" negate="true" />
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
</system.webServer>

通过使用其中一个在线正则表达式工具,我发现“
^localhost$
”是否与我的localhost url匹配

事实证明,事实并非如此

将正则表达式更改为简单的“
(localhost:)
”修复了它

我还删除了第二条规则,其中只包含localhost,因为它没有做任何事情,并且碰巧“removewwww”匹配url abit

以下是我的结论:

<system.webServer>    
  <rewrite>
      <rules>
        <rule name="AlwaysOn agent requests without any redirections" stopProcessing="true">
          <match url="^$"/>
          <conditions>
            <add input="{HTTP_USER_AGENT}" pattern="^AlwaysOn$"/>
          </conditions>
          <action type="None"/>
        </rule>
        <rule name="Remove www" stopProcessing="true">
          <match url="(.*)\/\/www\.(.*)$"></match>
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" appendQueryString="true" redirectType="Permanent" />
        </rule>
      <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
        <match url="^(.*)$"/>
        <conditions>
          <add input="{HTTPS}" pattern="^OFF$"/>
          <add input="{HTTP_HOST}" matchType="Pattern" pattern="(localhost:)" negate="true" /> 
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent"/>
      </rule>
      </rules>
    </rewrite>
</system.webServer>

从我的问题来看,这可能并不明显,但我更喜欢在web.config文件中执行。
<system.webServer>    
  <rewrite>
      <rules>
        <rule name="AlwaysOn agent requests without any redirections" stopProcessing="true">
          <match url="^$"/>
          <conditions>
            <add input="{HTTP_USER_AGENT}" pattern="^AlwaysOn$"/>
          </conditions>
          <action type="None"/>
        </rule>
        <rule name="Remove www" stopProcessing="true">
          <match url="(.*)\/\/www\.(.*)$"></match>
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" appendQueryString="true" redirectType="Permanent" />
        </rule>
      <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
        <match url="^(.*)$"/>
        <conditions>
          <add input="{HTTPS}" pattern="^OFF$"/>
          <add input="{HTTP_HOST}" matchType="Pattern" pattern="(localhost:)" negate="true" /> 
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent"/>
      </rule>
      </rules>
    </rewrite>
</system.webServer>