Redirecting not www to www rule causing a problem for files

I added this rule to my web.config to redirect non-www www addresses.

<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 works fine for the main site url. For example, if the user types are http://example.com , it is redirected to http://www.example.com

But for some urls like

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

he is redirected to

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

Here you can see 2 times www in the URL.

+4
source share
1 answer

I think this 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 does not work properly, you can try adding another condition:

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

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1403743/


All Articles