I have set up a redirect to the mobile version of my site when the user is on a mobile phone. However trying to open this on an iPhone it produces the error 'Safari cannot open the page because to many redirects occurred' and when opening it on an Android phone it says - 'this page has a redirect loop'.

I cannot seem to find where the redirect is looping. Below is the section of my .htaccess file that handles the redirect.

ErrorDocument 404 /404.htm

RewriteEngine On

RewriteBase /
# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:demo.1ecommerce.com]

# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
#  had to check, but I believe most mobile devices should send at
#  least one of these headers)
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^ http://demo.1ecommerce.com/index-mob.php [R,L]

Any help with this would be greatly appreciated!

Dani AI

Generated

Quick diagnosis and what likely went wrong: the .htaccess rule is redirecting mobile user‑agents to a mobile page on the same host but never reliably exits that rule — either because the cookie-based "skip" check never becomes true (mod_rewrite’s [CO] only sends Set-Cookie in the response, so the cookie isn’t present until the next request) or because the browser blocks/partitions the cookie (Safari/WebKit Intelligent Tracking Prevention and similar privacy settings can prevent cookies from being usable for this kind of cross-site or third-party flow). ’s cookie+subdomain idea is a valid direction, but it requires correct cookie domain/path and accounting for browser privacy rules; ’s generator tip is fine for syntax but won’t locate logic-level loops. (httpd.apache.org)

How to trace the loop (quick checklist): capture the exact redirect chain and headers with curl using a mobile user agent and verbose/follow options, inspect access/error logs to see repeated 3xx responses, and test with temporary 302s and a cleared browser (or private window) to avoid cached 301 behavior. Example diagnostic command:

curl -v -L -A 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1' 'http://example.com/'

This shows each Location and any Set-Cookie headers so it’s possible to spot the hop that sends the request back. (curl.se)

Common, low-risk fixes to try (server-side): exclude the mobile target from the redirect conditions so the rule won’t re-fire when the mobile page is already being served, or use an internal rewrite (no external redirect) to serve the mobile template without another HTTP round‑trip. Example exclusion:

RewriteCond %{REQUEST_URI} !^/index-mob\.php$
RewriteRule ^ /index-mob.php [L,R=302]

If switching to an m. subdomain, ensure cookie domain/path, host checks, and any CDN/SSL settings aren’t producing conflicting redirects (Cloudflare Flexible SSL and similar setups are a frequent cause of loops). (developers.cloudflare.com)

Extra caution: check the mobile page itself (PHP/server code) for any redirect back to the desktop site, and keep redirects temporary (302) while testing. Server logs plus the curl trace will show exactly where the loop begins and make the definitive fix obvious.

There are many .htaccess file generators are available online.

Search for .htaccess fiel generator online. Give proper values and you will get your new fil. Try it, it will be helpful for sure.

Give a try to this -

RewriteEngine On

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]

# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
#  had to check, but I believe most mobile devices should send at
#  least one of these headers)
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie}        !\smredir=0(;|$)
# Now redirect to the mobile site
RewriteRule ^  [R,L]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.