Redirect Desktop Internal Pages to Correct Mobile Internal Pages with Htaccess

Posted by Luis Alejandro Ramrez Gallardo on Stack Overflow See other posts from Stack Overflow or by Luis Alejandro Ramrez Gallardo
Published on 2013-06-25T16:18:34Z Indexed on 2013/06/25 16:21 UTC
Read the original article Hit count: 344

I have built a Mobile site in a sub-domain. I have successfully implemented the redirect 302 from:

www.domain.com to m.domain.com in htaccess.

What I'm looking to achieve now it to redirect users from:

www.domain.com/internal-page/ > 302 > m.domain.com/internal-page.html

Notice that URL name for desktop and mobile is not the same. The code I'm using looks like this:

# 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]
</IfModule>
# END WordPress

# Mobile Redirect
# Verify Desktop Version Parameter
RewriteCond %{QUERY_STRING} (^|&)ViewFullSite=true(&|$)

# Set cookie and expiration
RewriteRule ^ - [CO=mredir:0:www.domain.com:60]

# Prevent looping
RewriteCond %{HTTP_HOST} !^m.domain.com$

# Define Mobile agents
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC]

# Verify if not already in Mobile site
RewriteCond %{HTTP_HOST} !^m\.

# We need to read and write at the same time to set cookie
RewriteCond %{QUERY_STRING} !(^|&)ViewFullSite=true(&|$) 

# Verify that we previously haven't set the cookie
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]

# Now redirect the users to the Mobile Homepage
RewriteRule ^$ http://m.domain.com [R]
RewriteRule $/internal-page/ http://m.domain.com/internal-page.html [R,L]

© Stack Overflow or respective owner

Related posts about .htaccess

Related posts about redirect