Reputation: 114
This seems like it should be such a 101-level question but I cannot get it to work.
I very simply need to set up a url rewrite rule that redirects mydomain.com to www.mydomain.com. That's it. But no matter how I go at it, I get a URL redirect error in my browser.
Below are a few different rule definitions I have tried, one-by-one. As far as I can tell, any should work. But none have. I would be incredibly grateful for any insight.
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
<add input="{HTTP_HOST}" negate="true" pattern="^www.mydomain.com$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
Upvotes: 0
Views: 7487
Reputation: 4619
In IIS, it's built in as a canonical host redirect. See below to 301 redirect any request from http://example.com to http://www.example.com
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
Upvotes: 2