简体   繁体   中英

IIS web.config Redirect // www and HTTPS in dev environment

I have a .NET MVC 5 website where I am currently forcing https and changing www to non-www using the web config in my production environment (with a valid cert). This works fine. I also have two development environments. These environments have self-signed certs set up, and I want to force https and change www to non-www in those environments as well.

<rewrite>
  <rules>
    <rule name="Redirect any HTTP request to HTTPS with no www" patternSyntax="ECMAScript" stopProcessing="true">
      <match url=".*"></match>
      <conditions >
        <add input="{HTTP_HOST}" pattern="^(.*)?website.com$"></add>
        <add input="{HTTPS}" pattern="off"></add>
      </conditions>
      <action type="Redirect" url="https://website.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
    </rule>
    <rule name="Redirect https www to HTTPS with no www" patternSyntax="ECMAScript" stopProcessing="true">
      <match url=".*"></match>
      <conditions >
        <add input="{HTTP_HOST}" pattern="^www.website.com$"></add>
        <add input="{HTTPS}" pattern="on"></add>
      </conditions>
      <action type="Redirect" url="https://website.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
    </rule>
  </rules>
</rewrite>

However, my current implementation checks "HTTPS" = off/on, which is always "off" in my development environments because they don't have valid certificates. This causes an infinite redirect loop (TOO_MANY_REDIRECTS error in Chrome). I've tried all the macros I could find to check against ({URL}, {REQUEST_URL}, {HTTP_HOST}, etc...), but none of these are the fully qualified URL (ie: " http://website.dev/whatever "). If I had this, I could check if someone is trying to get to use https or http.

This isn't a big deal, but I've spent way too much time on it already, haha! Any ideas are appreciated!

{CACHE_URL} variable contains full url. You can build condition with that variable instead {HTTPS} . It should looks something like that:

Instead:

<add input="{HTTPS}" pattern="off"></add>

use:

<add input="{CACHE_URL}" pattern="^http://" />

And instead:

<add input="{HTTPS}" pattern="on"></add>

use:

<add input="{CACHE_URL}" pattern="^https://"  />

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