4
votes

I have added this rule in my web.config to redirect non www URLs to www.

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

Although it is working fine for main site URL. Like if user types http://example.com it redirects to http://www.example.com

But for some URLs like

http://www.example.com/userfiles/abc.pdf

it redirects to

http://www.www.example.com/userfiles/abc.pdf

Here you can see 2 time www in URL.

1

1 Answers

1
votes

I guess this one should work:

<rule name="Redirect to WWW" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
  </conditions>
  <action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>

If this one does not work as expected, you could try to add another condition:

<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

Hope this helps.