homepage Welcome to WebmasterWorld Guest from 54.163.89.8
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

    
I Need Multiple Mod Rewrite Rules for two mobile sites
TurboCoder3000



 
Msg#: 4549760 posted 8:25 am on Feb 28, 2013 (gmt 0)

I have two mobile websites - one for iPhone and one for iPad, 2 different designs stored in two different directories. I am trying to make it so that the following occurs. Unfortunately, just adding the two lines at the end of the RewriteCond code for iPad doesn't work.

1. When you visit on a phone device (iPhone, Android), the browser should go to: [mobile.example.com,...] which is the mobile site.
a. When you tap full website, it should go to the full site on the phone device.
2. When you visit the site on an iPad/Pad device, the browser should go to: http://www.example.com/mobile/ipad/
a. When you tap full website on the iPad, it should go to the full site http://www.example.com/ on the iPad device.

#Mobile detection
RewriteCond %{QUERY_STRING} !(.*&)?nomobile [NC]
RewriteCond %{REQUEST_URI} !\.*(jpg|png|css|this|that|otherthing)$ [NC]
RewriteCond %{HTTP_COOKIE} !^.*nomobile.*$ [NC]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
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://mobile.example.com [L,R=302]
RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://www.example.com/mobile/ipad/ [R=301]

 

g1smd

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



 
Msg#: 4549760 posted 9:17 am on Feb 28, 2013 (gmt 0)

(.*&)? simplifies to (^|&)

With leading \.* the * should be deleted.

Leading uncaptured ^.* is always redundant.

Trailing uncaptured .*$ is always redundant.

Since the iPad rule is "more specific" it should go first. However, it may also need various of the other RewriteCond lines to be duplicated with it.

Looking at the rest of the Conditions, several are mutually exclusive or redundant. In particular,

RewriteCond %{REQUEST_URI} !\.*(jpg|png|css|this|that|otherthing)$ [NC]
RewriteCond %{REQUEST_URI} ^/$

say "Redirect if this is NOT a request for an image or css, in fact redirect ONLY when root / is requested".

So, which is it: "not images and css" or "only root"?

TurboCoder3000



 
Msg#: 4549760 posted 9:46 am on Feb 28, 2013 (gmt 0)


Looking at the rest of the Conditions, several are mutually exclusive or redundant. In particular,

RewriteCond %{REQUEST_URI} !\.*(jpg|png|css|this|that|otherthing)$ [NC]
RewriteCond %{REQUEST_URI} ^/$

say "Redirect if this is NOT a request for an image or css, in fact redirect ONLY when root / is requested".

So, which is it: "not images and css" or "only root"?


I am not entirely sure - I didn't write the original code and don't have access to the original developer to find out.

I would imagine it's a redirect when root / is requested only, since this would override just about any image request below the root, correct?

lucy24

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



 
Msg#: 4549760 posted 12:07 pm on Feb 28, 2013 (gmt 0)

since this would override just about any image request below the root, correct?

I think you've misunderstood what "root" means. It doesn't mean "located in the top directory" or "associated with the front page". It means only, specifically, narrowly: The front page of the site. Nothing else. The one that shows up in your logs as

GET / HTTP et cetera.

Your rule has far, far too many conditions. Your server will spend its life assessing them and will never have time to serve up any content.

Within any group of conditions, list them in order of most-likely-to-fail. RewriteConds work on a sudden-death principle. (Apache has a fancier name, but that's what it means.) If one of your conditions involves the page name, put that in the rule itself. In fact almost all RewriteRules-- except the ones specifically concerned with hotlinking and similar-- can be constrained up front to requests for pages.

It's probably futile to list every telephone under the sun. They come up with a new one every other week. Just list the likeliest half-dozen or so, and let the others ask for your mobile site if they want it.

Unless your site is horrendously huge and complicated-- or it has a lot of finicky little things to click on-- the iPad may not even need special handling. In my experience its browser can take anything you throw at it.

I am not entirely sure - I didn't write the original code

Never mind what the code says. The question is: what do you want it to say?

TurboCoder3000



 
Msg#: 4549760 posted 3:01 pm on Feb 28, 2013 (gmt 0)

Never mind what the code says. The question is: what do you want it to say?


Ideally, I would like it to be more simple. Will this code work better instead of having all of those user agent declarations (acs|alav|alca|amoi|audi|aste|avan|benq, etc.)? Could I junk all of the user agent declarations and just have them say the following, so that if a user clicks on full site on an iPhone/android device w/the URL example.com?nomobile it will take them to the full site, and if they click on full site on an iPad w/the URL example.com?noipad it will take them to the full site?

RewriteEngine on
RewriteCond %{QUERY_STRING} !(.*&)?noipad [NC]
RewriteCond %{HTTP_COOKIE} !^.*noipad.*$ [NC]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} ipad [NC]
RewriteCond %{HTTP_HOST} !^example.com\. [NC]
RewriteRule ^ http://www.example.com/mobile/ipad/ [L,R=301]

RewriteEngine on
RewriteCond %{QUERY_STRING} !(.*&)?nomobile [NC]
RewriteCond %{HTTP_COOKIE} !^.*nomobile.*$ [NC]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} (iphone|android) [NC]
RewriteCond %{HTTP_HOST} !^example.com\. [NC]
RewriteRule ^ http://[mobile.example.com/ [L,R=301]

[edited by: incrediBILL at 3:41 am (utc) on Mar 2, 2013]
[edit reason] disabled smilies [/edit]

TurboCoder3000



 
Msg#: 4549760 posted 2:54 pm on Mar 1, 2013 (gmt 0)

Have I stumped everyone with my question? :)

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