0

The target IIS serves multiple domains via multiple sites and host header binding. So far when a new site (domain) was added I could create a specific rule for that domain to redirect example.com requests to www.example.com.

<rule name="Redirect to www" stopProcessing="true">
  <match url="(.*)" />
  <conditions >
    <add input="{HTTP_HOST}" pattern="^example.com" />
  </conditions>
  <action type="Redirect" 
    <-- it is OK to hardcode https, because http->https rewrite rule also in effect --!>, 
    url="https://www.example./{R:1}" />
</rule>

Question

Besides I am not sure the rule/condition/action above is entirely correct, now I would like to improve the solution to one universal rewrite rule. How can I add an universal global rule what will apply to all (future) domains, and redirects anysld.anytld/anyremainingpathandqueryandwhoknowswhat to www.anysld.anytld/anyremainingpathandqueryandwhoknowswhat?

g.pickardou
  • 32,346
  • 36
  • 123
  • 268

2 Answers2

1

If I understood your question correctly code from another SO question should help you. Take a look at IIS7 URL Rewrite - Add "www" prefix. Is this what you are looking for?

1

If you need to use global rules to redirect requests to www in all root domain, you can try the following global rule:

<rewrite>
    <globalRules>
         <rule name="Add www" stopProcessing="true">
                <match url="^(.*)$" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
                </conditions>
                <action type="Redirect" url="https://www.{C:0}" appendQueryString="false" />
          </rule>
    </globalRules>
</rewrite>
Xudong Peng
  • 1,463
  • 1
  • 4
  • 9