2
votes

I create a URL Rewrite Rule to remove the WWW from incoming requests based on http://madskristensen.net/post/url-rewrite-and-the-www-subdomain

Here is the rule straight from my web.config:

<rewrite>
  <rules>
    <rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{CACHE_URL}" pattern="*://www.*" />
      </conditions>
      <action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

When I try to open www.mydomain.com, FireFox gives me a "Corrupted Content Error" message. If I try to open it in Chrome, nothing happens.

Here are the response headers via Fiddler:

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: http://example.com:80:123.123.123.123/
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Mon, 07 Dec 2015 18:20:53 GMT
Content-Length: 167

Response body:

<head>
  <title>Document Moved</title>
</head>
<body>
  <h1>Object Moved</h1>This document may be found <a HREF="http://example.com:80:123.123.123.123/">here</a>
</body>

Notice how the port and IP address are included in the Location. (I have replaced the IP Address of my server with 123.123.123.123)

Is this causing the issue? If so, why is it including this information and how to I remove it?

I restarted IIS after installing URL Rewrite.

1
I'm facing this problem too. Any solutions?Marius Stănescu

1 Answers

1
votes

Not really a solution, but my workaround...

My site is setup to require SSL so I don't really need the benefit of removing WWW for both protocols.

I have updated my rule as follows:

<rule name="Remove WWW and Redirect to HTTPS" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
  </conditions>
  <action type="Redirect" url="https://{C:2}/{R:1}" />
</rule>