1

My site is hosted on IIS, I need to enforce the browser to use www prefix.

I've installed the Url Rewrite Module and my rule is:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
              <rule name="Add WWW" stopProcessing="true">
              <match url="^(.*)$" />
              <conditions>
                 <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
              </conditions>
              <action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
           </rule>
        </rules>
    </rewrite>
</system.webServer>

from IIS7 URL Rewrite - Add "www" prefix

But I cannot work out how to maintain ssl

Community
  • 1
  • 1
White Wolf
  • 47
  • 7
  • Welcome to stackoverflow, please consider [accepting](http://stackoverflow.com/help/accepted-answer) the answer or provide some feedback if I've misinterpreted your scenario or just plain got it wrong :-) – NikolaiDante Feb 16 '16 at 20:42

1 Answers1

1

You need to capture the protocol in the input:

<rule name="Enforce WWW" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
  </conditions>
  <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>

{C:1} will contain the protocol and {C:2} will have your domain and anything else.

(source)

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117