1

I'm trying to create an IIS rule to redirect mobile users to the mobile site. The main site (desktop version) is in the root route ('/'), and the mobile site is in the route '/mobile'

I created this IIS rule, but when I try it in desktop it's work well, but in mobile, I got an error ERR_TOO_MANY_REDIRECTS

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite Mobile" enabled="true" stopProcessing="true">
            <match url="(.*)" ignoreCase="true"/>
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
                <add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobile|phone" />
                <add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobile|phone" />
            </conditions>
            <action type="Redirect" url="mysiteurl/mobile" appendQueryString="false" />
        </rule>
    </rewrite>
  </system.webServer>
</configuration>
Netanel Stern
  • 149
  • 3
  • 16

1 Answers1

1

It is happening because your rule is also matching all mobile URLs. You need to exclude mobile URLs from your rule. This rule will work for you:

<rule name="Rewrite Mobile" enabled="true" stopProcessing="true">
    <match url="mobile(.*)" negate="true"/>
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
        <add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobile|phone" />
        <add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobile|phone" />
    </conditions>
    <action type="Redirect" url="/mobile" appendQueryString="false" />
</rule>
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36
  • it's work, but after the first time that I check the mobile version on my laptop, (I set mobile mode in chrome developer tools), and then, every time that I try to access to the root route ('/') I get the mobile ('/mobile') – Netanel Stern Nov 02 '17 at 11:28
  • Because usually, the browser is caching redirects. You need to clear cache – Victor Leontyev Nov 02 '17 at 11:30
  • If my solution is working for you, can you please accept my asnwer – Victor Leontyev Nov 02 '17 at 11:38
  • 1. The cache is disabled on the specific tab 2. How can I prevent the browser to cache this redirect? – Netanel Stern Nov 02 '17 at 13:52
  • Browsers are always caching redirects. You can find more details in this thread: https://stackoverflow.com/questions/9130422/how-long-do-browsers-cache-http-301s – Victor Leontyev Nov 02 '17 at 13:59