简体   繁体   中英

Redirect to https without www, while skipping localhost

I'm trying to setup rewrite rules in a Asp.Net project.

Rewriting to https and removing any "www." currently works in production, but when I try to run it on localhost, it keeps redirecting it to https.

It's an Umbraco site running on Azure. Which is why the first rule is there.

This is what it currently looks like: I have both a rule and two conditions trying to skip the redirection of localhost, but neither of them works.

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

I figured it out, by using one of those online regex tools, to see if " ^localhost$ " was matching my localhost url.

And it turned out that it didnt.

Changing the regex to a simple " (localhost:) " fixed it.

I also removed the second rule with just localhost in it, because it wasn't doing anything, and chanced the "Remove www" match url abit.

Here is what I ended up with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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