Rewrite to HTTPS with subdomain?

Asked by At

How can I setup web.config to handle the following redirects

  1. http://example.com -> https://example.sub.com
  2. http://www.example.com -> https://example.sub.com
  3. http://example.sub.com -> https://example.sub.com

The part I am not sure about is matching the urls, for example,

http://<dynamic> to http://<dynamic>.sub.com

1 Answers

0
Seferan On

I would solve this with two rules. One will match when the hostname contains .sub.com and simply perform a plain redirect. The next will append .sub.com to the hostname.

These are based off #4 of https://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/#redirect-https

Finally, after you've proved that everything works as expected, changed the Found to Permanent in both rules.

<rule name="Redirect to HTTPS" stopProcessing="true">  
    <match url="(.*)" />  
    <conditions>  
        <add input="{HTTPS}" pattern="^OFF$" />  
        <add input="{HTTP_HOST}" pattern="\.sub\.com$" />  
    </conditions>  
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />  
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">  
    <match url="(.*)" />  
    <conditions>  
        <add input="{HTTPS}" pattern="^OFF$" />    
    </conditions>  
    <action type="Redirect" url="https://{HTTP_HOST}.sub.com/{R:1}" redirectType="Found"     />  
</rule>