0

I'm using IIS 7 to host a web application. Currently, I have a rule in the web.config that re-directs all traffic to HTTPS, however it doesn't automatically redirect traffic to www which is important for some functionality. The web.config is shown below:

<rewrite>
      <rules>
          <rule name="HTTP to HTTPS redirect" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                  <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                  <add input="{URL}" pattern="seek\.svc$" ignoreCase="true" negate="true" />
              </conditions>
              <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
          </rule>
      </rules>
  </rewrite>

Can anyone tell me how to redirect all requests to www? Either using this route or using the IIS GUI.

I've found this: IIS 6 - Setting up 301 redirect for non-www to www for SEO

Which does the trick, but creating a duplicate site seems a little messy to me. Are there any other suggestions?

thanks

Paul
  • 119
  • 7

1 Answers1

1

Make a second ruleset before the existing one, without the stopProcessing flag, and add a condition that matches the www. before anything else and set the negate flag to true, like this:

<add input="{HTTP_HOST}" pattern="^www\.([.a-zA-Z0-9]+)$" negate="true" />

then add the www. to the url of your action like this:

<action type="Redirect" redirectType="Found" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" />
Mathias R. Jessen
  • 24,907
  • 4
  • 62
  • 95