Search code examples
iis-10url-rewrite-module

Troubleshoot Redirect Loop affecting only Mobile Devices


I'm using IIS10 and URL-Rewrite and for some reason my website has a redirect-loop but ONLY on mobile devices. No matter which mobile, what network it's connected on or how many times I clear cache, the site won't load on any browser for any mobile device. I've tested using iphone, android, chrome, firefox, safari it's all the same.

Here are my rule(s) in the web.config

<rewrite>
      <rules>
        <rule name="CanonicalHostNameRule1">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
            <add input="{HTTP_HOST}" pattern="^(.*)\.example\.com$" negate="true" />
          </conditions>
          <action type="Redirect" url="http://example.com/{R:1}" />
        </rule>
        <rule name="Redirect to HTTPS" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
        </rule>
      </rules>
    </rewrite>

I have other websites hosted on the same machine with similar URL rewrite rules that work fine no problems.

Even with all my rules turned OFF, I'm still getting a redirect loop on mobile.

Any tools, tips/tricks on how to troubleshoot this would be most appreciated!


UPDATE: Info from Fiddler

RAW:

HTTP/1.1 301 Moved Permanently
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: http://example.com/
X-Redirect-Reason: Wrong Portal Alias Requested
Set-Cookie: dnn_IsMobile=True; path=/; HttpOnly
Set-Cookie: language=en-US; path=/; HttpOnly
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Date: Thu, 29 Oct 2020 13:19:56 GMT
Content-Length: 142

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://example.com/">here</a>.</h2>
</body></html>

Headers:

**Response Headers**
HTTP/1.1 301 Moved Permanently
**Cache**
Cache-Control: private
Date: Thu, 29 Oct 2020 13:19:56 GMT
**Cookies/Login**
Set-Cookie: dnn_IsMobile=True; path=/; HttpOnly
Set-Cookie: language=en-US; path=/; HttpOnly
**Entity**
Content-Length: 142
Content-Type: text/html; charset=utf-8
**Miscellaneous**
X-Redirect-Reason: Wrong Portal Alias Requested
**Security**
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
**Transport**
Location: http://example.com/

I see Wrong Portal Alias Requested but not sure what would cause that?


Solution

  • Credit to LexLi who got me looking in the right direction. Fiddler was very helpful.

    Yes, the website is constructed on the DNN CMS application, the problem was that in DNN there is a table portalsettings that has a setting called DefaultPortalAlias there was a wrong value in this setting.

    Additionally in DNN's PortalAlias table there are bit fields for IsPrimary and there the IsPrimary in my case was set to the www. version of the alias not the non-www. and in IIS my url rewrite canonical rule was point to the non-www. binding, this is what caused the loop. I'm not sure why it was only affecting mobile but it's working now after those changes.

    Thanks @Lex Li for the insights!