简体   繁体   中英

Easy IIS 7.5 url rewrite rule (from non-www to www) gives redirect loop error in browser

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM