How to create a redirect from non-www to www for IIS 7.0+

If you have IIS 7 installed with the web redirection module, you can use the following snippet to redirect http:// to http://www with one simple rule added to your system.webServer web.config section.

<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
    <add input="{HTTP_HOST}" pattern="^$" />
<action type="Redirect" url="{R:0}" redirectType="Permanent" />

If you have a https website to contend with and you need all the traffic to go to the https version, use:


    <rule name="Redirect to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^$" />
      <action type="Redirect"
        url="{MapProtocol:{HTTPS}}://{R:1}" />
    <rewriteMap name="MapProtocol">
      <add key="on" value="https" />
      <add key="off" value="http" />

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.