2
votes

Hey there StackOverflow Community, i searched a lot on stackoverflow to fix my "too many redirect" Error. But couldn't find anything that helps. I want to redirect to a mobile directory via .htaccess and the UserAgent Condition. Found a high voted Solution that looks like this:

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|ipad|iemobile" [NC]
RewriteRule ^(.*)$ http://mysite.com/mobile/$1 [R=301,L]

The redirect itself works great but its seems like the mobile browsers add /mobile in a loop so the URL grows into something like http://mysite.com/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile till i get an Error "couldn't open site because of too many redirects".

My complete .htaccess looks like this:

AddType video/ogg  .ogv
AddType video/mp4  .mp4
AddType video/webm .webm

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|ipad|iemobile" [NC]
RewriteRule ^(.*)$ http://mysite.com/mobile/$1 [R=301,L]

Thanks in Advance, Lucas Tito

1
Happened the same many times. Is it possible that the htaccess is inside some folder into mysite.com/mobile ? This would be a recursive redirection.opalenzuela

1 Answers

2
votes

Replace your RewriteRule with this:

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|ipad|iemobile" [NC]
RewriteRule ^((?!mobile/).*)$ /mobile/$1 [R=301,L]

Problem is that you're unconditionally prefixing /mobile/ before any URL even ones that already start with /mobile/.