【问题标题】:Redirect to https without www, while skipping localhost在跳过 localhost 时重定向到不带 www 的 https
【发布时间】:2020-05-29 07:56:54
【问题描述】:

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

重写为 https 并删除任何 "www." 目前在生产中有效,但是当我尝试在 localhost 上运行它时,它不断将其重定向到 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>

【问题讨论】:

    标签: c# .net azure url-rewriting web-config


    【解决方案1】:
    void Application_BeginRequest(Object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.IsLocal)
            return;
    
        if (!Request.IsSecureConnection)//always https!!!
        {
            var _nfn = "https://" + (Request.Url.Host + Request.RawUrl).ToLowerInvariant();
            Response.RedirectPermanent(_nfn);
            return;
        }
    }
    

    【讨论】:

    • 也许我的问题并不明显,但我更愿意在 web.config 文件中进行。
    【解决方案2】:

    我发现,通过使用其中一种在线正则表达式工具来查看“^localhost$”是否与我的本地主机 URL 匹配。

    事实证明它没有。

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

    我还删除了仅包含 localhost 的第二条规则,因为它没有做任何事情,并且偶然出现了“删除 www”匹配 url。

    这是我最终得到的结果:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 2020-05-22
      • 2012-06-09
      相关资源
      最近更新 更多