Redirecting all URLs to their HTTPS WWW equivalents

1 minute read

Whether you’re a no-www or a yes-www person (you can guess which one I am based on the URL currently present on your address bar 🙂), one thing is certain – you need to be consistent. This means that if you like www, then should redirect (301 permanent) to, and if you don’t like it then the redirection needs to go the other way around. Either way, you can’t have people browsing the same page from different domain (with www and without it), it’s just confusing.

In addition, you really should be using SSL everywhere, all the time. Certificates are free, and even if your hosting plan doesn’t support them (as is the case with Azure’s free tier), you can have CloudFlare take care of that for you (and enjoy improved performance and security as a bonus). Once you have SSL up and running, you’d want to redirect all HTTP traffic to HTTPS and enable HSTS for increased security.

To recap – once you’ve decided whether you like www or not, and finished setting up SSL, you want your redirections to look like this (assuming yes-www):

  • HTTP:// -> HTTPS://
  • HTTP:// -> HTTPS://
  • HTTPS:// -> HTTPS://

In addition, you want HSTS headers (Strict_Transport_Security) in place. Fortunately, using the IIS Rewrite module (installed by default on Azure deployments), accomplishing all of the above is a breeze:

   <rule name="Redirect to www" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
     <add input="{HTTP_HOST}" pattern="^yourdomain\.com"/>
     <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
    <action type="Redirect" 
   <rule name="HSTS" enabled="true">
       pattern=".*" />
     <add input="{HTTPS}" pattern="on" ignoreCase="true" />
    <action type="Rewrite" value="max-age=31536000" />

Happy rewriting 😉

Leave a Comment