Solved

.htaccess rule to redirect to mobile URI help needed

Posted on 2015-02-05
2
248 Views
Last Modified: 2015-02-06
Hello, I am needing to redirect website visitors to a mobile site based on the user agent information. I've found below code in research and works ok for iphone and android, but I need to redirect to equivalent pages between the desktop and mobile site. For example, if a mobile visitor lands on www.website.com/page5.html, they need to redirect to m.website.com/page5.html. The below code only redirects mobile visitors to  http://m.website.com.

I've experimented with adding:

RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ http://m.website.com%{REQUEST_URI} [R,L]

Open in new window


in a few different ways to the below code, but doesn't seem to work. Does anyone know of how I can alter the below working code to redirect to specific pages / URIs?

Thank You!

Current Code:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [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]

# Now redirect to the mobile site
RewriteRule ^ http://m.website.com [R,L]

Open in new window

0
Comment
Question by:Mark B
2 Comments
 
LVL 13

Accepted Solution

by:
Mark Bullock earned 500 total points
ID: 40593005
To change the hostname, but keep the pathname, replace your last line with:
RewriteRule  ^/(.*)  http://m.website.com/$1  [R,L]

Open in new window

0
 

Author Closing Comment

by:Mark B
ID: 40593613
Thank you, that worked!
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now