Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
What do you mean "redirect all to www" ? –  Stephane Jan 17 '12 at 14:39

1 Answer 1

up vote 1 down vote accepted

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" />
share|improve this answer
    
Thanks, works perfectly! –  Paul Jan 17 '12 at 14:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.