How to redirect dynamic urls of my desktop site to mobile subdomain?

I have created a mobile site and redirected it with .htaccess to mobile subdomain. For Example my site is example.com and a mobile urls is m.example.com, Now when I open a specific URL apart from index like example.com/product.html, Let’s say products, It’s opening a homepage m.example.com, I wanna open m.example.com/product.html. How to do it with .htacces

Desktop Site htaccess code

#Options +FollowSymLinks  
Options -MultiViews
RewriteEngine On  
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{HTTP_HOST} www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteBase /

RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.rxcues.com]

# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
#  had to check, but I believe most mobile devices should send at
#  least one of these headers)
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]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

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


RewriteRule index.html templateouter.php
RewriteRule index.php templateouter.php
RewriteRule error.html error.php

#RewriteRule portfolio-([a-z0-9-]+).html$ portfolio.php?pctid=$1

#RewriteRule ^([a-zA-Z0-9-/]+).html/?$ template.php?url=$1.html&module=$2 [QSA,L]


RewriteRule ^(gallery|hire|quote|pages|staff)/([A-Za-z0-9-]+)/?$ template.php?page=$2&module=$1 [QSA,L]
RewriteRule ^(quote|hire)/?$ template.php?page=$1&module=$1 [QSA,L]

RewriteRule ^([a-zA-Z0-9-/]+).html$ template.php?url=$1.html

RewriteRule ^([a-zA-Z0-9-/]+)/place-(.*) template.php?url=$1.html&place=$2 [QSA,L]

RewriteRule ^([a-zA-Z0-9-/]+)/(.*)/search-(.*) template.php?url=$1.html&id=$2&name=$3 [QSA,L]

RewriteRule ^([a-zA-Z0-9-/]+)/(.*)/(.*)/(.*) template.php?url=$1.html&id=$2&name=$3&other_name=$4 [QSA,L]

For mobile subdomain .htacces is below

Options +FollowSymLinks  
Options -MultiViews
RewriteEngine On  
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteBase /
RewriteRule index.html template.php
RewriteRule index.php template.php
RewriteRule error.html error.php


RewriteRule ^([a-zA-Z0-9-/]+).html$ template.php?url=$1.html

RewriteRule ^([a-zA-Z0-9-/]+)/place-(.*) template.php?url=$1.html&place=$2 [QSA,L]

RewriteRule ^([a-zA-Z0-9-/]+)/(.*)/search-(.*) template.php?url=$1.html&id=$2&name=$3 [QSA,L]

RewriteRule ^([a-zA-Z0-9-/]+)/(.*)/(.*)/(.*) template.php?url=$1.html&id=$2&name=$3&other_name=$4 [QSA,L]


Source: .htaccess

Leave a Reply