简体   繁体   中英

Replace word in URL before redirect to mobile with htaccess

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]

Try this :

RewriteEngine On
RewriteCond %{HTTP_HOST} !^m.example.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
RewriteRule ^reportage/(.+)$   http://m.example.com/post/$1   [L]

Note : I haven't tested the above,if you find it not working, feel free to comment

For changing :

www.mysite.com/en/photoreportage/5000 

To :

m.mysite.com/en/post/5000

try :

RewriteEngine On
    RewriteCond %{HTTP_HOST} !^m.example.com$ [NC]
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
    RewriteRule ^photoreportage/(.+)$   http://m.example.com/en/post/$1   [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM