White Wolf
White Wolf

Reputation: 47

How do I enforce www on an IIS hosted website?

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

Upvotes: 1

Views: 230

Answers (1)

NikolaiDante
NikolaiDante

Reputation: 18639

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)

Upvotes: 1

Related Questions