Kunal Patel
Kunal Patel

Reputation: 95

IIS Rewrite rule for non-www to www not working

We have a bunch of websites hosted in IIS using Flex. There are quite a lot of Rewrite/Redirect rules for each of them. One of them now needs a Rewrite rule to direct a non-www url to a www url. Have already tried quite a few popular solutions from various sites including other SO question. The last I tried is as following, without much to avail:

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

Upvotes: 0

Views: 761

Answers (1)

Abraham Qian
Abraham Qian

Reputation: 7522

  1. Ensure you have the latest URL Rewrite extension.
    https://www.iis.net/downloads/microsoft/url-rewrite
  2. Before testing the result, please clean the local cache. We had better verify it in an inprivate window.

Besides, Failed Request Tracing is a powerful tool for troubleshooting request-processing failures. FRT can be used with the URL rewrite module to trace how rewrite rules were applied to the request URL.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules
At last, please try the below configuration.

<rule name="Force www" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^example\.com$" />
        </conditions>
        <action type="Redirect" url="https://www.example.com{REQUEST_URI}" />
</rule>

Feel free to let me know if the problem still exists.

Upvotes: 1

Related Questions