Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.config redirect all requests to https://www

Tags:

iis

web-config

We want to redirect all incoming requests to https://www.domain.com

  • http://domain.com -> https://www.domain.com
  • http://www.domain.com -> https://www.domain.com
  • https://domain.com -> https://www.domain.com
  • https://www.domain.com -> https://www.domain.com

We have tried dozens of code snippes from the web but none covered all the examples from above.

Can anyone help with an appropriate web.config file for IIS?

like image 856
libjup Avatar asked Sep 09 '26 13:09

libjup


2 Answers

I got the same problem. This rule works for me.

    <rewrite>
        <rules>
            <clear />
            <rule name="Redirect non-www OR non-https to https://www">
                <match url=".*" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_HOST}" pattern="^domain.com$" />
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" url="https://www.domain.com/{R:0}" redirectType="Permanent"/>
            </rule>
        </rules>
    </rewrite>
like image 166
user123457 Avatar answered Sep 15 '26 00:09

user123457


The best way to this is via your DNS records + some IIS configuration.

  • Add a CName that redirects domain.com to www.domain.com
  • In IIS, create a "fake" website and edit its binding to handle http://www.domain.com
  • Configure an HTTP redirection in the web.config of this "fake" website (or through the UI) to redirect requests coming from the host name www.domain.com to https://www.domain.com$S$Q ($S and $Q allows you to keep the parameters after the redirect, see this link for more info)
  • Finally, remove the HTTP binding your website (only use https binding on this one).
like image 28
Superzadeh Avatar answered Sep 15 '26 02:09

Superzadeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!