简体   繁体   中英

Web.config URL redirect to non-www + https

Currently, my rule is:

<rule name="SecureRedirect" stopProcessing="true">
  <match url="^(.*)$" />
  <conditions>
    <add input="{HTTPS}" pattern="off" />
    <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
  </conditions>
  <action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>

The problem is here:

http://www.domainName.com/image.png redirects wrongly to https://domainName.com instead of https://domainName.com/image.png

and

https://www.domainName.com/image.png doesn't ever redirect to https://domainName.com/image.png

So, what's the true way to redirect all to non-www https URL?

The correct rule, which will fit all your requirements is:

<rule name="SecureRedirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
         <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
         <add input="{HTTPS}" pattern="off" />
    </conditions>
    <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>

The way I did it on my site is as follows:

ServerName www.example.com
ServerAlias example.com
Redirect / https://www.example.com/

Try this rule:

<rule name="SecureRedirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>

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