Recently I had to configure some redirections in a website that contains two different domains.
Both use https and I want the main site, which uses the primary domain, to only be accessible through it’s www version. For the second site, since it’s a sub-domain, I only want it to only be available in https.
Let’s say the domain is mydomain.com and the subdomain is mysub.mydomain.com. The only URLs I want to be available are:
- https://www.mydomain.com
- https://mysub.mydomain.com
Typically I would make the non-www to www redirection through the DNS provider, but for this website it was not possible.
The following is the configuration I used in my Web.config:
<rewrite> <rules> <rule name="Redirect from non www mydomain.com" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^mydomain.com$" /> </conditions> <action type="Redirect" url="https://www.mydomain.com/{R:0}" redirectType="Permanent" /> </rule> <rule name="Redirect from non https mydomain.com" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> <add input="{HTTP_HOST}" pattern="^www.mydomain.com$" /> </conditions> <action type="Redirect" url="https://www.mydomain.com/{R:0}" redirectType="Permanent" /> </rule> <rule name="Redirect from non https mysub.mydomain.com" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> <add input="{HTTP_HOST}" pattern="^mysub.mydomain.com$" /> </conditions> <action type="Redirect" url="https://mysub.mydomain.com/{R:0}" redirectType="Permanent" /> </rule> </rules> </rewrite>
Hope this helps!