RewriteCond 排除不工作的路径 [英] RewriteCond to exclude a path not working

查看:24
本文介绍了RewriteCond 排除不工作的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这个网址:

http://www.example.com/path/antyhing

不被重定向.

以下是我无法使用的:

RewriteCond %{REQUEST_URI} !^/path/.*$ [NC]
RewriteRule ^ http://m.example.com/ [R,L]

目前它将所有 URL 重定向到 http://m.example.com/

Currently it redirects all URLs to http://m.example.com/

这是我的 .htaccess 文件中的完整代码:

Here is the full code in my .htaccess file:

RewriteBase /
RewriteEngine On

# 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}]

# Skip next rule if mobile=0
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|ipad|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(;|$)
# Don't redirect "path" pages
RewriteCond %{REQUEST_URI} !^.+?/path/.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com/ [R,L]

推荐答案

你能用这个替换你的最后一条规则吗:

Can you replace your last rule with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# 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}]

# Skip next rule if mobile=0 [OR] if it's a file [OR] if /path/
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$) [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^.*/path/.*$
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device
RewriteCond %{HTTP_PROFILE}       !^$ [OR]
RewriteCond %{HTTP_X_WAP_PROFILE} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
# 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(;|$)
# Don't redirect "path" pages
RewriteCond %{REQUEST_URI} !^.*/path/.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com/ [R,L,NC]

由 OP 编辑​​: 唯一的问题来自 %{REQUEST_URI},我不明白它只对 ^ 有效.*/path/.*$

Edit by OP: The only problem was coming from the %{REQUEST_URI} that for a reason that I don't understand only works against ^.*/path/.*$

这篇关于RewriteCond 排除不工作的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆