0

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>
Sapphireblue
  • 104
  • 2
  • 15

1 Answers1

2

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>
Ryan Nigro
  • 4,389
  • 2
  • 17
  • 23
  • I am not sure I understand what is meant to be "built in" here, but this rule, too, results in a redirect loop error for me. – Sapphireblue Nov 25 '13 at 03:54
  • 2
    I just mean that there's a wizard for it in IIS under the URL Rewrite module. Click to add a new rule, and a canonical redirect is one of the options. I'm certain what I have above is correct (I pulled it from one of my websites). If you're seeing a redirect loop, then I'd guess that your browser is caching the 301 redirect. I'd clear browser cache, close your browser, and retest. – Ryan Nigro Nov 25 '13 at 04:01
  • Oh, I see. I actually don't really use the rewrite GUI much (maybe I should, if my hand-edits don't work!). Really appreciate your clarification, but I've been clearing browsing data every time and same result. Just doublechecked to be sure. Any ideas what else could be going on? This particular site runs a bunch of hand-rolled ColdFusion code that I'm 99% sure contains no redirects (Application.cfm shows nothing suspect), as well as a WordPress multi-site install (but that's another directory level down from the root where I'm testing this). – Sapphireblue Nov 25 '13 at 04:08
  • oh and: I have zero other rules in place here at the root directory of this site. – Sapphireblue Nov 25 '13 at 04:11
  • Smells like something other than the IIS URLRewrite then to me. Have you tested without the rewrite rule in the web.config? If you still see any redirection there, then something else is at play. – Ryan Nigro Nov 25 '13 at 04:11
  • I have. The page loads as expected, with browser url bar showing the non-www address. – Sapphireblue Nov 25 '13 at 04:13
  • What about when you go to the www address without the rewrite rule? Does it redirect back to the non-www? – Ryan Nigro Nov 25 '13 at 04:13
  • No; it remains as the www address, and loads normally. (On edit: just used the built-in GUI tool I hadn't known existed in order to create my rule; same result.) – Sapphireblue Nov 25 '13 at 04:16
  • Hmm. My money's on something to do with Wordpress overwriting the HTTP_HOST server variable. Are http://wordpress.org/support/topic/wrong-http_host-causes-infinite-redirect or http://stackoverflow.com/questions/2297403/http-host-vs-server-name relevant? – Ryan Nigro Nov 25 '13 at 04:22
  • ! Maybe. Echoing HTTP_HOST from a PHP file at the site root gives me non-www. I'm trying to go digging now for where that might be getting set... (that question is 5 years old and the canonical.php file mentioned doesn't exist anymore in WP. but this seems like a fantastic lead.) – Sapphireblue Nov 25 '13 at 04:36
  • That's your answer then. If the HTTP_HOST is always the non-www version, then that would result in an infinite loop with the code above. I'm out for the night. Good luck chasing it down. – Ryan Nigro Nov 25 '13 at 04:39
  • Lord. This is incredibly embarrassing, but I requested my HTTP_HOST output test file *from* the non-www address. Requesting it from the www address shows me the www address as expected—so I'm back to being stumped. – Sapphireblue Nov 25 '13 at 05:19
  • I still have no clarity on my problem but I'm going to accept this answer because it seems like the rule given above SHOULD work on most server environments, and will probably work for anyone who isn't me. Thanks Ryan. – Sapphireblue Nov 25 '13 at 15:56