我遇到了很多关于基于HTTP查询的重定向的问题,当然还有匹配URL的部分,但并不是两者都有。
我的目录结构如下:
- http://localhost/common_parent/.htaccess
- http://localhost/common_parent/demo/themes/
- http://localhost/common_parent/themes/?ajax=1
我所做的重写工作很好,只是它阻塞了第二个列表项中的目录:
RewriteCond %{REQUEST_URI} !.xml$
RewriteRule ^[^/].*/themes(.+) themes$1 [L]
不幸的是,demo/themes/?ajax=1
和themes/?ajax=1
都是用ajax
HTTP查询请求的。然而,使用foo
HTTP查询仅请求demo/themes/
。
- http://localhost/common_parent/demo/themes/?ajax=1&foo=条形
- http://localhost/common_parent/themes/?ajax=1
简而言之,我如何重写我的rewrite
,使其仅在ajax
http查询存在且同时foo
http查询不存在的情况下应用?
您可以使用这样的重写规则:
# when ajax=<whatever> query parameter is present
RewriteCond %{QUERY_STRING} (?:^|&)ajax= [NC]
# when foo=<whatver> is not present
RewriteCond %{QUERY_STRING} !(?:^|&)foo= [NC]
# when URI is not ending with .xml
RewriteCond %{REQUEST_URI} !.xml$ [NC]
# rewrite handler
RewriteRule ^[^/].*/themes(.+) themes$1 [L]