Showing posts with label php Replace word in URL before redirect to mobile with htaccess. Show all posts
Showing posts with label php Replace word in URL before redirect to mobile with htaccess. Show all posts

Replace word in URL before redirect to mobile with htaccess

Tuesday, November 3, 2015

I want to know how can I replace a word in URL when redirect from desktop to mobile, redirection is realized with htaccess.



For example http://www.example.com/reportage/2000
change to m.example.com/post/2000



This is my htaccess code for redirection.



RewriteRule ^admin($|/) - [L]

# Check if this is the noredirect query string
#RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
# Set a cookie, and skip the next rule
#RewriteRule ^ - [CO=mredir:0:www.example.com]

# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile} !^$

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie} !\mobile=0(;|$)
# Now redirect to the mobile site
#Redirect 301 ^/photoreportage /post
RewriteRule ^ http://m.example.com%{REQUEST_URI} [R,L]