Forum Moderators: phranque

Message Too Old, No Replies

Redirect subfolder to subdomain and force subdomain for mobile devices

Forcing a subdomain for mobile devices/redirect subfolder to subdomain

         

The Beerocrat

8:26 am on Jun 13, 2010 (gmt 0)

10+ Year Member



Hello all,

I'm trying to do two different things with my .htaccess file. First of all, I want to redirect mobile browsers to a special mobile version of my site. I have a list of mobile browsers and other devices (like iPads and whatnot) that I can match on HTTP_ACCEPT and HTTP_USER_AGENT. This is the URL redirect I want (I use an MVC framework on my site):

www.domain.com/controller/view/var1/var2/ --> m.domain.com/controller/view/var1/var2/

I also want to do this for consoles like the Wii, where the subdomain will be wii.domain.com.

Secondly, I have to store the subdomain information in a subfolder on my server so that m.domain.com actually lives on www.domain.com/versions/mobile. However, even though people will not likely visit the longer version of the URL, I'd like to force it to use the shorter one, like so:

www.domain.com/versions/mobile/controller/view/var1/var2/ --> m.domain.com/controller/view/var1/var2

For both of these, the number of variables after the controller/view section will vary, so it will need to account for 0+ variables on the end.

I know next to nothing about .htaccess files and regexes, so it's difficult for me to really wrap my head around other answers and mold them into my own needs. I do know that I'm forcing all requests to domain.com to change to www.domain.com, and I don't know if that will be affected by my two other changes above.

jdMorgan

3:59 pm on Jun 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To get you started:

Both mod_alias and mod_rewrite can be used to generate client redirects.
mod_alias does not support conditional redirects, except those directly-based on the requested URL-path.
Howver, HTTP_ACCEPT and HTTP_USER_AGENT can be tested by the RewriteCond directive of mod_rewrite.
Query strings pass through RewriteRules unchanged by default, and thus pose no particular challenge here.
The rest of this is simple subdomain-URL to subdomain-filepath mapping, already covered extensively here.

The resources cited in our Forum Charter may prove useful in interpreting the above comments.

Having reviewed this materaal, please post your best effort at coding a solution, to serve as a basis for discussion.

Thanks,
Jim

The Beerocrat

7:20 am on Jun 14, 2010 (gmt 0)

10+ Year Member



Jim,

Thanks for your links. They helped me understand a little bit more about Apache and mod_rewrite.

Here's my attempt:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^m.domain.com$
RewriteCond %{HTTP_HOST} !^wii.domain.com$
RewriteCond %{HTTP_USER_AGENT} "ipad|nintendo wii|playstation 3" [NC]
RewriteRule ^(.*)$ wii.domain.com [L,R=302]

RewriteCond %{HTTP_HOST} !^m.domain.com$
RewriteCond %{HTTP_HOST} !^wii.domain.com$
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|erics|hipt|inno|ipaq|iphone|ipod|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|nintendo dsi|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|playstation portable|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|windows ce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteRule ^(.*)$ m.domain.com [L,R=302]

RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^versions/mobile/(.*)$ m.domain.com [L,R=302]

RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^versions/console/(.*)$ wii.domain.com [L,R=302]


Will this do what I want it to?

g1smd

7:28 am on Jun 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The redirect target probably needs the protocol prefix adding.

I don't think www.domain.com will be seen as being a domain name without it.

The Beerocrat

7:54 pm on Jun 14, 2010 (gmt 0)

10+ Year Member



So is this what I need to do?

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} !^m.example.com$
RewriteCond %{HTTP_HOST} !^wii.example.com$
RewriteCond %{HTTP_USER_AGENT} "ipad|nintendo wii|playstation 3" [NC]
RewriteRule ^/(.*)$ http://wii.example.com [L,R=302]

RewriteCond %{HTTP_HOST} !^m.example.com$
RewriteCond %{HTTP_HOST} !^wii.example.com$
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|erics|hipt|inno|ipaq|iphone|ipod|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|nintendo dsi|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|playstation portable|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|windows ce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteRule ^/(.*)$ http://m.example.com [L,R=302]

RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/versions/mobile/(.*)$ http://m.example.com [L,R=302]

RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/versions/console/(.*)$ http://wii.example.com [L,R=302]

[edited by: jdMorgan at 10:59 pm (utc) on Jun 14, 2010]
[edit reason] example.com [/edit]

jdMorgan

11:13 pm on Jun 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fix the rule order to generate the desired 301 redirects on /versions/ requests.

Use 303 (See Other) redirects to avoid the "moved" semantics of 301 and 302. This is not a relocated resource, but rather, an alternative representation.

RewriteEngine on
#
RewriteBase /
#
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^/versions/mobile/(.*)$ http://m.example.com/$1 [L,R=301]
#
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^/versions/console/(.*)$ http://wii.example.com/$1 [L,R=301]
#
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} !^wii\.example\.com
RewriteCond %{HTTP_USER_AGENT} ipad|nintendo\ wii|playstation\ 3 [NC]
RewriteRule ^/(.*)$ http://wii.example.com/$1 [L,R=303]
#
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} !^wii\.example\.com
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|erics|hipt|inno|ipaq|iphone|ipod|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|nintendo\ dsi|noki|opwv [NC,OR]
RewriteCond %{HTTP_USER_AGENT} palm|pana|pant|pdxg|phil|playstation\ portable|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|windows\ ce|iemobile|mini|mmp [NC,OR]
RewriteCond %{HTTP_USER_AGENT} symbian|midp|wap|phone|pocket|mobile|pda|psp [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteRule ^/(.*)$ http://m.example.com/$1 [L,R=303]

I have put the $1 path back into all rules to avoid redirecting "all" pages requested by a mobile device to the root of the mobile site(s). This would not be a good idea for SEO purposes, or even for users. If you will not have a mobile page for each "regular" page, then there are two choices:

1) Do not redirect all mobile device requests from non-mobile to mobile. Only redirect 'home page' requests and/or requests for pages which you know exist in the mobile versions.

2) Do not redirect all mobile device requests from non-mobile to mobile. Instead, check the /versions filepath using the requested URL-path appended to the correct /versions/device-type/ filepath to see if the file exists in the device-specific part of your filesystem.

Jim

The Beerocrat

3:11 am on Jun 15, 2010 (gmt 0)

10+ Year Member



Thank you for the response. Unfortunately it doesn't seem to be working. My .htaccess file doesn't want to redirect my Android or my Wii to my homepage. I even messed up my automatic prepending of www. on my domain, which I have since fixed. I can't seem to figure out why it's not working.

It may help if my entire .htaccess file is posted:

# Make index.php the directory index page
DirectoryIndex index.php

# Block directory listings
Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Force WWW in front of URL
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} !^wii\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

# Redirect mobile subfolder to mobile subdomain
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^/versions/mobile/(.*)$ http://m.example.com/$1 [L,R=301]

# Redirect console subfolder to console subdomain
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^/versions/console/(.*)$ http://wii.example.com/$1 [L,R=301]

# Redirect WWW access from console to console subdomain
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} !^wii\.example\.com
RewriteCond %{HTTP_USER_AGENT} ipad|nintendo\ wii|playstation\ 3 [NC]
RewriteRule ^/(.*)$ http://wii.example.com/$1 [L,R=303]

# Redirect WWW access from mobile to mobile subdomain
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} !^wii\.example\.com
RewriteCond %{HTTP_ACCEPT} text/vnd\.wap.wml|application/vnd\.wap\.xhtml\+xml [NC,OR]
RewriteCond %{HTTP_USER_AGENT} acs|alav|alca|amoi|andr|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd- [NC,OR]
RewriteCond %{HTTP_USER_AGENT} dang|doco|erics|hipt|inno|ipaq|iphone|ipod|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|nintendo\ dsi|noki|opwv [NC,OR]
RewriteCond %{HTTP_USER_AGENT} palm|pana|pant|pdxg|phil|playstation\ portable|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|windows\ ce|iemobile|mini|mmp [NC,OR]
RewriteCond %{HTTP_USER_AGENT} symbian|midp|wap|phone|pocket|mobile|pda|psp [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteRule ^/(.*)$ http://m.example.com/$1 [L,R=303]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>


Also, I do plan on having equivalent pages with exact URLs for every subdomain; the only difference will be the subdomain itself.

Once again, thanks for your help everybody. I'm really trying hard to understand what all these rewrite rules do.

jdMorgan

12:54 am on Jun 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rules need to be re-ordered, first of all.

Put all external redirects first, in order from most-specific patterns and conditions (fewest URLs affected) to least-specific (more URLs affected).
Follow with all internal rewrites, again in order from most-specific patterns and conditions.

This will avoid multiple/stacked/chained redirects when one request has several things 'wrong' with it, and will avoid exposing previously-rewritten filepaths as URLs to the client (which is really bad for search engine ranking.)

For example, your first rule posted above should actually be your last external redirect rule, since it affect *all* URLs requested from non-canonical domains. And for use in .htaccess, this rule can be more efficiently coded as

# Force WWW in hostname
RewriteCond %{HTTP_HOST} !^(www|m|wii)\.example\.com\.?(:[0-9]+)?)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]


The first two internal rewrite rules are broken. Move the requested-URL-path pattern to the RewriteRule, and delete the RewriteCond.
 RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

becomes
 RewriteRule ^(system.*)$ /index.php?/$1 [L] 

These two existing rules are broken because the value of "%{REQUEST_URI}" always starts with a slash, so the patterns in these rules will never match.

If this code is going into .htaccess, then you will need several more changes. First of all, remove the leading slash from all RewriteRule patterns.

The last rule posted above is very inefficient. Each request causes your hard drive to be checked one to three times, and disk accesses are slow. So consider excluding index.php requests and requests for image, css, and JavaScript files from the disk checks using one or two additional RewriteConds at the top.

You can get rid of the <IfModule> container, unless you want this code to fail silently if mod_rewrite is not installed.

Jim

The Beerocrat

6:53 am on Jun 16, 2010 (gmt 0)

10+ Year Member



Jim, thank you so much for your help! The .htaccess rewrite rules work perfectly now.

Here's what I did:

# Make index.php the directory index page
DirectoryIndex index.php

# Block directory listings
Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On

## EXTERNAL NARROW ##
# Redirect mobile subfolder to mobile subdomain
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^versions/mobile/(.*)$ http://m.example.com/$1 [L,R=301]

## EXTERNAL NARROW ##
# Redirect console subfolder to console subdomain
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^versions/console/(.*)$ http://wii.example.com/$1 [L,R=301]

## EXTERNAL BROAD ##
# Redirect WWW access from console to console subdomain
RewriteCond %{HTTP_HOST} !^(m|wii)\.example\.com
RewriteCond %{HTTP_USER_AGENT} ipad|nintendo\ wii|playstation\ 3 [NC]
RewriteRule ^(.*)$ http://wii.example.com/$1 [L,R=303]

## EXTERNAL BROAD ##
# Redirect WWW access from mobile to mobile subdomain
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} !^wii\.example\.com
RewriteCond %{HTTP_ACCEPT} text/vnd\.wap.wml|application/vnd\.wap\.xhtml\+xml [NC,OR]
RewriteCond %{HTTP_USER_AGENT} acs|alav|alca|amoi|andr|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd- [NC,OR]
RewriteCond %{HTTP_USER_AGENT} dang|doco|erics|hipt|inno|ipaq|iphone|ipod|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|nintendo\ dsi|noki|opwv [NC,OR]
RewriteCond %{HTTP_USER_AGENT} palm|pana|pant|pdxg|phil|playstation\ portable|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|windows\ ce|iemobile|mini|mmp [NC,OR]
RewriteCond %{HTTP_USER_AGENT} symbian|midp|wap|phone|pocket|mobile|pda|psp [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteRule ^(.*)$ http://m.example.com/$1 [L,R=303]

## EXTERNAL BROAD ##
# Force WWW in hostname
RewriteCond %{HTTP_HOST} !^(www|m|wii)\.example\.com\.?(:[0-9]+)?)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

## INTERNAL NARROW ##
# Removes access to the system folder by users.
RewriteRule ^(system.*)$ /index.php?/$1 [L]

## INTERNAL NARROW ##
# Removes access to the application folder by users.
RewriteRule ^(application.*)$ /index.php?/$1 [L]

## INTERNAL BROAD ##
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>


As you can see, I chose to keep the inefficient broad internal redirect for the time being (I'm not too worried about server performance at this time). I also kept the <IfModule> even though my server does have the rewrite module installed.

Once again, thanks for all the help.

jdMorgan

12:58 pm on Jun 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> As you can see, I chose to keep the inefficient broad internal redirect for the time being (I'm not too worried about server performance at this time).

You should be... Every clock cycle you waste now is one that you will have to work hard to recover later...
I commented on this specific bit of code because it has such a huge impact on server/site performance (and possibly on the lifespan of your hard drive) that failing/forgetting to fix it now could be a disaster. Sorry-strong feelings on this one...

> I also kept the <IfModule> even though my server does have the rewrite module installed.
Again, I'm not sure why you would want this code to fail silently --fail to work but not even cause an error log entry-- on a server without mod_rewrite support. Including the <IfModule> is primarily useful only when code is to be deployed across a number of servers with different configurations, and you do not wish to be bothered if the code cannot execute on a particular server. In most situations, that would be a big mistake, since it makes finding the problem quite a bit more difficult. As a result, checking for the module's presence on each and every HTTP request is just a waste of time with little (if any) benefit.

A few more tweaks (only modified rules shown here):

## EXTERNAL BROAD ##
# Redirect WWW access from mobile to mobile subdomain
[b]RewriteCond %{HTTP_HOST} !^(m|wii)\.example\.com
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC][/b]
RewriteCond %{HTTP_ACCEPT} text/vnd\.wap.wml|application/vnd\.wap\.xhtml\+xml [NC,OR]
RewriteCond %{HTTP_USER_AGENT} acs|alav|alca|amoi|andr|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd- [NC,OR]
RewriteCond %{HTTP_USER_AGENT} dang|doco|erics|hipt|inno|ipaq|iphone|ipod|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|nintendo\ dsi|noki|opwv [NC,OR]
RewriteCond %{HTTP_USER_AGENT} palm|pana|pant|pdxg|phil|playstation\ portable|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|windows\ ce|iemobile|mini|mmp [NC,OR]
RewriteCond %{HTTP_USER_AGENT} symbian|midp|wap|phone|pocket|mobile|pda|psp [NC]
RewriteRule ^(.*)$ http://m.example.com/$1 [L,R=303]
#
## EXTERNAL BROAD ##
# Force WWW in hostname
RewriteCond %{HTTP_HOST} !^(www|m|wii)\.example\.com\.?(:[0-9]+)?)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#
# Last and least-specific external redirect above, first and most-specific internal rewrite below
#
## INTERNAL NARROW ##
[b]# Removes access to the system and application folders by users
RewriteRule ^((system|application).*)$ /index.php?/$1 [L][/b]
#
## INTERNAL BROAD ##
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
[b]RewriteRule $1 !(^index\.php|\.(gif|jpe?g|png|ico|css|js))$[/b]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

I suggest that you NOT use 'shorthand' in your comments. While the meaning of "External" and "Broad" may seem obvious now, being more descriptive may prove useful in the future. It would also be a "clue" to someone else working on the code that the distinctions are important. Good comments are highly-correlated with good code, and bad comments with buggy code. If one does not understand the function and the goal of a particular routine well enough to describe it in words, then it's likely that one cannot code it well either. And if a function is too complex to be described concisely in a reasonably-short comment, then that is a clue that the function needs to be simplified or to be broken down into a series of sub-functions (e.g. subroutines).

I seem to be on a soap-box this morning... I guess 35+ years of coding does that to you... :)

Jim

mgorman03

12:51 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



Guys,

Been reading through this and wow .... I am lost. I tried to word with the code above on my server but it goes into giant redirect mode that never stops.

On my server, I am running cpane and set up a subdomain called mobile.stimulent.com and that goes to a folder called www.stimulent.com/mobile

That is where my files will reside. There are just some test files there now.

Here is my code. Any thoughts or inputs on what I might be doing wrong ? I have not used a lot of .htaccess files in the past so this is a little new to me.

Mark

<IfModule mod_rewrite.c>
RewriteEngine On

## EXTERNAL NARROW ##
# Redirect mobile subfolder to mobile subdomain
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^versions/mobile/(.*)$ [mobile.stimulent.com...] [L,R=301]

## EXTERNAL NARROW ##
# Redirect console subfolder to console subdomain
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^versions/console/(.*)$ [wii.stimulent.com...] [L,R=301]

## EXTERNAL BROAD ##
# Redirect WWW access from console to console subdomain
RewriteCond %{HTTP_HOST} !^(mobile|wii)\.example\.com
RewriteCond %{HTTP_USER_AGENT} ipad|nintendo\ wii|playstation\ 3 [NC]
RewriteRule ^(.*)$ [wii.stimulent.com...] [L,R=303]

## EXTERNAL BROAD ##
# Redirect WWW access from mobile to mobile subdomain
RewriteCond %{HTTP_HOST} !^mobile\.stimulent\.com
RewriteCond %{HTTP_HOST} !^wii\.stimulent\.com
RewriteCond %{HTTP_ACCEPT} text/vnd\.wap.wml|application/vnd\.wap\.xhtml\+xml [NC,OR]
RewriteCond %{HTTP_USER_AGENT} acs|alav|alca|amoi|andr|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd- [NC,OR]
RewriteCond %{HTTP_USER_AGENT} dang|doco|erics|hipt|inno|ipaq|iphone|ipod|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|nintendo\ dsi|noki|opwv [NC,OR]
RewriteCond %{HTTP_USER_AGENT} palm|pana|pant|pdxg|phil|playstation\ portable|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|windows\ ce|iemobile|mini|mmp [NC,OR]
RewriteCond %{HTTP_USER_AGENT} symbian|midp|wap|phone|pocket|mobile|pda|psp [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteRule ^(.*)$ [mobile.stimulent.com...] [L,R=303]

## EXTERNAL BROAD ##
# Force WWW in hostname
RewriteCond %{HTTP_HOST} !^(www|m|wii)\.example\.com\.?(:[0-9]+)?)?$
RewriteRule ^(.*)$ [stimulent.com...] [L,R=301]


## INTERNAL BROAD ##
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html?/$1 [L]
</IfModule>

mgorman03

12:58 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



Just noticed something ....

when it redirects on my iphone it goes to www.stimulent.com/mobile/mobile/mobile/mobile/mobile/mobile/mobile/ ...

and so on ... I also fixed the one line up there that said "example" on it.

jdMorgan

5:27 pm on Sep 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change "example" to "stimulent" in *all* cases to start. I assume that you know that the word is properly spelled "stimulant" as well...

I see no references to a subdirectory named "/mobile" in this code. Therefore, the problem likely also involves code in another config file or .htaccess file.

Jim

mgorman03

6:07 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



Jim, I actually made the change of the "example" text above after I posted this. Stimulent is that way we spell it ... long story.

On my server side, I went into cpanel and set up a subdomain and the subdomain redirects to www.stimulent.com/mobile and that is all through the cpanel interface. Should there be a .htaccess file or anything in that folder or subdomain ?

if I go to mobile.stimulent.com from a normal web browser all is fine.

Thanks for your help, Mark

jdMorgan

6:49 pm on Sep 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the significance of the subdomain "m" in this rule's RewriteCond?

Either it should have been "mobile" instead of "m", or you should add "mobile" to this list of subdomains...

## EXTERNAL BROAD ##
# Force WWW in hostname
RewriteCond %{HTTP_HOST} !^(www|m|wii)\.example\.com\.?(:[0-9]+)?)?$
RewriteRule ^(.*)$ http://www.stimulent.com/$1 [L,R=301]

Jim