Ali
Ali

Reputation: 2042

Mobile redirect and looping error

I'm working on small CMS, I changed all originals directories to my desired directories through RewriteRule in htaccess, Also add mobile template for mobile device users. So my website has two directories, one for Computer client and one for Mobile client, I using the following code to detect mobiles agent and redirect to mobile template but looping errors occur, Any ideas !?

This is my redirect code:

RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /m/ [L,R=302]

This is my current htaccess:

Options +FollowSymLinks
RewriteEngine On
# Change main URL 
RewriteRule ^m/ /mobile/index.php [QSA]
# Change content URL
RewriteRule ^/a/m/([a-zA-Z0-9\.]+)$ /mobile/index.php?pid=$1 [QSA]

Upvotes: 2

Views: 159

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

You need to check that the URI also isn't the rewritten URI (/mobile/...). You can do that by either checking against %{THE_REQUEST} or include a check for /mobile:

RewriteCond %{THE_REQUEST} !\ /m/.*
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /m/ [L,R=302]

or:

RewriteCond %{REQUEST_URI} !^/a/m/.*$
RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /m/ [L,R=302]

Upvotes: 1

Related Questions