homepage Welcome to WebmasterWorld Guest from 184.73.40.21
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
too many redirects in htaccess
joejohn



 
Msg#: 4650391 posted 8:25 pm on Mar 1, 2014 (gmt 0)

Hi guys
I am so glad I discovered this forum. I am having some problems with my htaccess code. I know nothing about this language.

I want to accomplish the following:
redirect non-www to www for all users
redirect desktop users to www.example.com/homepage
redirect mobile users to www.example.com/m

Here is my htaccess code. The problem is, if I am on an iphone and I type www.example.com it DOES direct to /m BUT if I dont put www at the beginning, it says too many redirects and the page doesn't open. there might be more things wrong with the code, I just pieced this together from different websites and dont know what I'm doing, please help.


RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /homepage/ [R=301]
</IfModule>

RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iPod.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iemobile.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*blackberry.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]


 

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4650391 posted 8:58 pm on Mar 1, 2014 (gmt 0)

Welcome to WebmasterWorld, joejohn!


you need to add an [L] flag for every 301.
then reorder your RewriteRules from most specific to most general.
all target URLs for 301s should include the full canonical protocol and hostname.

lucy24

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



 
Msg#: 4650391 posted 9:41 pm on Mar 1, 2014 (gmt 0)

Ah, it's the familiar "RewriteRules cobbled together from multiple sources" file. Get rid of the IfModule envelope. Ordinarily I'd say just the envelope, not its contents. But here you should ditch the whole thing; it's what is causing your infinite loop. Or part of what's causing, at least. The central line will come later.

Picking one set at random:
RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iPod.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]

Initial .* is never appropriate and here it isn't even necessary.
Opening and closing anchors are only needed when you're matching specific text that has to be in a particular location.
There is no need to capture unless you're going to reuse-- which, in this case, you are going to do, only you forgot to say so. I smell mod_alias syntax trying to sneak in where it isn't wanted.

The whole package can be reduced to

RewriteCond %{HTTP_USER_AGENT} iP([oa]d|hone)
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule (.*) http://www.example.com/m/$1 [R=301,L]

You have to capture the requested URL because otherwise everyone is forcibly sent to the /m/ page and can't go anywhere else.

And then, after you've done all the mobile redirects, the leftover non-mobiles will be picked up with

RewriteCond %{REQUEST_URI} !^/(homepage|m)/
RewriteRule ^$ http://wwww.example.com/homepage/ [R=301,L]


The [R] flag does not carry an implied L, so you have to say it explicitly every time.

Now then... What about people who don't want to be redirected to the mobile site? Especially the ones on iPads; they may prefer to stay on the "real" site. Where's the part that reads a cookie relating to redirect preferences, and only redirects the people who have expressed a willingness to be redirected?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4650391 posted 10:26 pm on Mar 1, 2014 (gmt 0)

As the number of user agents continues to increase by many hundreds per year, and the capabilities of mobile devices begin to rival desktop devices of only a few years ago, content generation decisions are moving away from user agent detection and instead over to display size considerations. The trend is moving away from having separate sites for different classes of devices and instead implementing a responsive design that adapts depending on screeen size and capabilities.

joejohn



 
Msg#: 4650391 posted 12:13 am on Mar 2, 2014 (gmt 0)

Hi guys

sorry but I have no idea what any of this means. I only know html and a little bit of php so your instructions is all greek to me.

like I said, I want 3 things accomplished:
redirect non-www to www for all users
redirect desktop users to www.example.com/homepage
redirect mobile users to www.example.com/m

I was hoping I could get a full htaccess code that I could just copy and paste to htaccess and be done with it.

lucy24

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



 
Msg#: 4650391 posted 2:40 am on Mar 2, 2014 (gmt 0)

I was hoping I could get a full htaccess code that I could just copy and paste

Not here, sorry. :( You could try That Other Forum; they're more oriented towards giving explicit answers.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4650391 posted 3:44 am on Mar 2, 2014 (gmt 0)

http://www.webmasterworld.com/apache/charter.htm [webmasterworld.com]:
"Fix my code" and "Do my homework for me" threads:

This is a discussion forum, not a help desk or a free code-writing service;
...
Rather than providing one-off solutions, we prefer to help people find resources to help themselves.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved