apache 如何有条件地让我的 .htaccess 跳过规则?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1035953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make my .htaccess skip rules conditionally?
提问by pixeline
I want to redirect all URLs that do not contain "_js","_css", "_img", etc., to my dispatch script. Somehow it doesn't work. For instance, all files inside my /_js/ folder are unaccessible (meaning: they are sent to index.php instead of getting to the physical files residing in that folder).
我想将所有不包含“_js”、“_css”、“_img”等的 URL 重定向到我的调度脚本。不知何故它不起作用。例如,我的 /_js/ 文件夹中的所有文件都无法访问(意思是:它们被发送到 index.php 而不是访问驻留在该文件夹中的物理文件)。
Here is my htaccess:
这是我的 htaccess:
IndexIgnore *
Options +FollowSymLinks
RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{REQUEST_URI} ^/(_admin/|_css/|_js/|_img/)
RewriteRule . - [S=9]
# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9._\-]+/)*[a-z0-9_\-]+)$ http://%{HTTP_HOST}//?%{QUERY_STRING} [NC,R,L]
# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=&item=&menu=&content=&id=&title=&%{QUERY_STRING} [NC,L]
# FIVE PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=&item=&menu=&content=&id=&%{QUERY_STRING} [NC,L]
# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=&item=&menu=&content=&%{QUERY_STRING} [NC,L]
# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=&item=&menu=&%{QUERY_STRING} [NC,L]
# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=&%{QUERY_STRING} [NC,L]
# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=&item=&%{QUERY_STRING} [NC,L]
# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([a-z0-9_\-]+)/$ index.php?tag=&%{QUERY_STRING} [NC,L]
# ONE PARAM
RewriteRule ^([a-z0-9_\-]+)/$ index.php?section=&%{QUERY_STRING} [NC,L]
Edit:
编辑:
Note my folder structure. Could it be the problem source?
注意我的文件夹结构。会不会是问题的根源?
I have a "v1" and "v2" folder structure. This .htaccess sits in the "v2" folder. one level above, i have a .htaccess that redirects all requests to "v2".
我有一个“v1”和“v2”文件夹结构。此 .htaccess 位于“v2”文件夹中。上一层,我有一个 .htaccess 将所有请求重定向到“v2”。
root
L.htaccess << dispatch between v1 and v2 folders
L v1 L v2 L.htaccess << the .htaccess code posted above
L _admin L all my website files & folders
回答by Gumbo
You're using the wrong character, |(broken bar, U+00A6) instead of |(vertical line, U+007C), and wrong pattern for REQUEST_URI.
您使用了错误的字符,|(断线,U+00A6)而不是|(垂直线,U+007C),并且REQUEST_URI.
RewriteCond %{REQUEST_URI} ^/v2/(_admin/|_css/|_js/|_img/)
RewriteRule . - [S=9]
Or for the .htaccess file in your v2directory just:
或者对于v2目录中的 .htaccess 文件:
RewriteRule ^_(admin|css|js|img)/ - [S=9]
回答by Vinko Vrsalovic
Here are some random fixes that may or may not fix the actual problem, which is not totally clear (see my comment) and which could be helped if you control the server and enable the RewriteLog, via:
以下是一些随机修复,可能会或可能不会解决实际问题,这些修复尚不完全清楚(请参阅我的评论),如果您通过以下方式控制服务器并启用 RewriteLog,可能会有所帮助:
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
but you have to put this in main server configuration.
但是你必须把它放在主服务器配置中。
That said, two main problems:
也就是说,两个主要问题:
- Lack of use of QSA flag (not really relevant)
- Excessive use of slashes (probably relevant in the actual problem, see new version of the skipping rule)
- 缺乏使用 QSA 标志(不是真正相关)
- 过度使用斜杠(可能与实际问题有关,请参阅新版本的跳过规则)
Here goes the modified file
这是修改后的文件
IndexIgnore *
Options +FollowSymLinks
RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{REQUEST_URI} ^(_admin|_css|_js|_img)
RewriteRule . - [L]
# Externally redirect to add missing trailing slash. Not really needed, AFAICS
# RewriteRule ^(([^/]+/)*[^/]+)$ http://%{HTTP_HOST}// [NC,R,L,QSA]
# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=&item=&menu=&content=&id=&title= [NC,L,QSA]
# FIVE PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=&item=&menu=&content=&id= [NC,L,QSA]
# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=&item=&menu=&content= [NC,L,QSA]
# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=&item=&menu= [NC,L,QSA]
# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/?$ index.php?section=downloads&item= [NC,L,QSA]
# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=&item= [NC,L,QSA]
# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([^/]+)/?$ index.php?tag= [NC,L,QSA]
# ONE PARAM
RewriteRule ^([^/]+)/?$ index.php?section= [NC,L,QSA]
EDIT: Given the folder structure explained, try adding to the v2 .htaccess at the beginning the following:
编辑:鉴于所解释的文件夹结构,请尝试将以下内容添加到 v2 .htaccess 开头:
RewriteBase /
You still haven't explained if you can or cannot use RewriteLog (I presume you can't)
你仍然没有解释你是否可以使用 RewriteLog(我认为你不能)

