user3196160
user3196160

Reputation: 37

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?

Upvotes: 1

Views: 892

Answers (3)

A Israfil
A Israfil

Reputation: 519

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

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

Upvotes: 0

Victor Leontyev
Victor Leontyev

Reputation: 8736

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>

Upvotes: 1

krlzlx
krlzlx

Reputation: 5832

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>

Upvotes: 0

Related Questions