在使用htaccess重定向到移动设备之前替换URL中的单词

时间:2015-11-03 07:23:49

标签: php .htaccess

我想知道当从桌面重定向到移动时如何替换URL中的单词,使用htaccess实现重定向。

例如http://www.example.com/reportage/2000 更改为m.example.com/post/2000

这是我的重定向htaccess代码。

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]

1 个答案:

答案 0 :(得分:0)

试试这个:

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]

注意:我没有对上述内容进行测试,如果发现它不起作用,请随时发表评论

更改:

www.mysite.com/en/photoreportage/5000 

致:

m.mysite.com/en/post/5000

尝试:

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]