Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

HELP! mobile site redirect loop somewhere

my htaccess file has a loop somewhere, can someone point it out



3:27 pm on Jul 1, 2011 (gmt 0)

Hello Apache wiz's!

I'm sure I am making a noob mistake in my htaccess file. I am trying to detect a mobile device and send the user to a my mobi subdirectory.

When I view my site on phone I get an error message saying that "Data Connectivity Problem: The page contains too many server redirects."

Here's my code:

#Redirect all non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

#redirect mobile users to mobile site

RewriteEngine On
RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,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] #*SEE NOTE BELOW
RewriteRule ^(.*)$ http://mobi.mysite.com/ [L,R=302]

I look forward to learning my mistake!


8:54 pm on Jul 1, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

You need to add a RewriteCond saying (in English) "if the request doesn't already contain "mobi.example.com". Unfortunately the [L] doesn't mean "Get thee hence and never set foot in this .htaccess file again for any reason." (If I try to explain it more coherently, I will be sorry.)


2:40 pm on Jul 2, 2011 (gmt 0)

Thanks for the reply. Unfortunately, it is a pretty vague response. Can you be more specific?

I have looked in forum after forum and web search after web search.

I have been copying and pasting what is working for others and then swapping out my info. I think somewhere in this process I blew it because I have a sub directory and the canonical url rewrite.


1:58 pm on Jul 12, 2011 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The RewriteRule [L] flag means, "stop processing right here for this pass through the mod_rewrite code if this rule matches. However, in a per-directory .htaccess context, mod_rewrite processing is always restarted after any rule matches, another pass will be performed, and this will continue until no more rules match. Only then will mod_rewrite truly "exit."

Therefore, as lucy24 points out, it is necessary to add a RewriteCond to your rule that stops the redirect if the requested hostname is already "mobi.mysite.com"

RewriteCond %{HTTP_HOST} !^mobi\.mysite\.com

For the sake of efficiency, I'd make that the first RewriteCond for your rule. Note that "!" means "NOT".

"Copying and pasting" is not recommended. Mod_rewrite is a powerful and therefore a potentially very-dangerous tool. Do not use mod_rewrite if you do not fully understand how it works and what the effects of each rule will be to both the server itself and to "users" such as people and search engine robots. One tiny little typo or oversight in your code can ruin your search engine rankings and/or drive off customers who experience problems.

I recommend that you study the mod_rewrite documentation and references cited in our Apache Forum Charter -- spending 3 days or a week with that documentation may save you years of frustration and serious technical problems with your site -- and it may save you from putting yourself out of business. We have, unfortunately, seen that happen here...



2:40 pm on Jul 12, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

The other problem you'll encounter is that your rules are in the wrong order.

You redirect requests to www and then you rewrite the request to serve the content. Just as the hard drive is about to send the content out, suddenly you want to redirect some requests to a different URL.

By then you have already rewritten the internal pointer and if you redirect the request now, you will expose that rewritten pointer back out on to the web as a new URL.


8:05 pm on Jul 27, 2011 (gmt 0)


I just joined the forum so that I could throw some light onto the problem. I too have been running in circles with a similar issue and found a fix and is working for me.

The problem is the .htaccess file applies to everything within its directory including all other directories/folders within, so the problem is you are identifying the user agent and telling it to redirect to another directory within , but as soon as the request reaches the destination folder it executes the original rule again resulting in the constant loop.

With that explanation aside the solution is as follows:

Create a second .htaccess file with the following:

#Begin user agent loop fix

RewriteEngine Off
RewriteBase /

#End user agent loop fix

Now place this .htaccess file in your destination directory / folder where you want the user to actually be.

Now when the original request is executed and reaches the destination it tells it not to execute any more rules.

Simple as that!

Hope this helps everyone out there that has been pulling their hair out :)


12:02 am on Jul 28, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Or add an exclusion to the original ruleset to stop the loop.


6:15 pm on Oct 21, 2011 (gmt 0)

Thanks for all the great responses! I clearly need to read the Charter as Jim pointed out.

I'm going to try applying what I learn and what you have mentioned in this thread.

This stuff is more technical than I'm used too.

Wish me luck. Thanks!



6:40 am on Oct 22, 2011 (gmt 0)

Hi All

I have learned a few things about htaccess since my original post, so I thought I would share my most recent code which works for sub-domains and does not suffer from the endless loop & an added bonus of having a link on your mobi site that will give users the option to view the full desktop site.

#BEGIN Mobile Redirects

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$)
RewriteRule ^ - [CO=mredir:0:www.yourdomain.com]
RewriteCond %{HTTP_USER_AGENT} "BlackBerry|Mobile|Opera|Palm|Symbian|Android|iPhone|iPod|iPad" [NC]
RewriteCond %{HTTP_HOST} !^mobile\.
RewriteCond %{QUERY_STRING} !(^|&)noredirect=true(&|$)
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301,L]

#END Mobile Redirects

**Replace "yourdomain.com" with your domain. Put a link on your mobi site to your desktop site and append the following on the end: /?noredirect=true


A last point to consider is that you don't need 100's of different user agents in your code, the above should cover about 98% of handsets being used out there.

I hope this helps



7:01 am on Oct 22, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

... and make sure this code goes near the beginning of the mod_rewrite code.

Ideally it will be located right after your RewriteRule(s) which block bad requests and it MUST be before any code with performs any internal rewrite on the request.

!^.*mredir=0.*$ is inefficient. The ^.* part causes multiple "back off and retry" trial matches slowing the server. The .*$ part wastes yet more processor cycles reading to the end. Use !mredir=0 instead.

The target URL (the root of mobile site) must end in a trailing slash.

Featured Threads

Hot Threads This Week

Hot Threads This Month