如何让我的 .htaccess 有条件地跳过规则?

发布于 2024-07-25 17:52:49 字数 1992 浏览 2 评论 0原文

我想将所有不包含“_js”、“_css”、“_img”等的URL重定向到我的调度脚本。 不知怎的,它不起作用。 例如,我的 /_js/ 文件夹中的所有文件都无法访问(这意味着:它们被发送到index.php,而不是访问该文件夹中的物理文件)。

这是我的 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}/$1/?%{QUERY_STRING} [NC,R,L]

# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6&%{QUERY_STRING} [NC,L]

# FIVE  PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&%{QUERY_STRING} [NC,L]

# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&%{QUERY_STRING} [NC,L]

# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&%{QUERY_STRING} [NC,L]

# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=$1&%{QUERY_STRING}  [NC,L]

# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&%{QUERY_STRING} [NC,L]

# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([a-z0-9_\-]+)/$ index.php?tag=$1&%{QUERY_STRING} [NC,L]
# ONE PARAM
RewriteRule ^([a-z0-9_\-]+)/$ index.php?section=$1&%{QUERY_STRING} [NC,L]

编辑:

注意我的文件夹结构。 这可能是问题的根源吗?

我有一个“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

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).

Here is my 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}/$1/?%{QUERY_STRING} [NC,R,L]

# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6&%{QUERY_STRING} [NC,L]

# FIVE  PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&%{QUERY_STRING} [NC,L]

# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&%{QUERY_STRING} [NC,L]

# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&%{QUERY_STRING} [NC,L]

# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=$1&%{QUERY_STRING}  [NC,L]

# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&%{QUERY_STRING} [NC,L]

# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([a-z0-9_\-]+)/$ index.php?tag=$1&%{QUERY_STRING} [NC,L]
# ONE PARAM
RewriteRule ^([a-z0-9_\-]+)/$ index.php?section=$1&%{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".

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2024-08-01 17:52:49

您使用了错误的字符,ι断线,U+00A6)而不是 |竖线) >、U+007C),并且 REQUEST_URI 模式错误。

RewriteCond %{REQUEST_URI} ^/v2/(_admin/|_css/|_js/|_img/)
RewriteRule . - [S=9]

或者对于 v2 目录中的 .htaccess 文件:

RewriteRule ^_(admin|css|js|img)/ - [S=9]

You’re using the wrong character, ¦ (broken bar, U+00A6) instead of | (vertical line, U+007C), and wrong pattern for REQUEST_URI.

RewriteCond %{REQUEST_URI} ^/v2/(_admin/|_css/|_js/|_img/)
RewriteRule . - [S=9]

Or for the .htaccess file in your v2 directory just:

RewriteRule ^_(admin|css|js|img)/ - [S=9]
沐歌 2024-08-01 17:52:49

这里有一些随机修复,可能会或可能不会解决实际问题,这并不完全清楚(请参阅我的评论),如果您控制服务器并启用 RewriteLog,则可能会有所帮助,通过:

RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9

但您必须将其放在 main 中服务器配置。

也就是说,有两个主要问题:

  • 缺乏使用 QSA 标志(并不真正相关)
  • 过多使用斜杠(可能与实际问题相关,请参阅新版本的跳过规则)

这是修改后的文件

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}/$1/ [NC,R,L,QSA]

# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6 [NC,L,QSA]

# FIVE  PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5 [NC,L,QSA]

# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4 [NC,L,QSA]

# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3 [NC,L,QSA]

# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/?$ index.php?section=downloads&item=$1  [NC,L,QSA]

# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2 [NC,L,QSA]

# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([^/]+)/?$ index.php?tag=$1 [NC,L,QSA]
# ONE PARAM
RewriteRule ^([^/]+)/?$ index.php?section=$1 [NC,L,QSA]

编辑:给出解释的文件夹结构,尝试在 v2 .htaccess 开头添加以下内容:

RewriteBase /

您仍然没有解释是否可以或不能使用 RewriteLog (我认为您不能)

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 "/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)

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}/$1/ [NC,R,L,QSA]

# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6 [NC,L,QSA]

# FIVE  PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5 [NC,L,QSA]

# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4 [NC,L,QSA]

# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3 [NC,L,QSA]

# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/?$ index.php?section=downloads&item=$1  [NC,L,QSA]

# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2 [NC,L,QSA]

# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([^/]+)/?$ index.php?tag=$1 [NC,L,QSA]
# ONE PARAM
RewriteRule ^([^/]+)/?$ index.php?section=$1 [NC,L,QSA]

EDIT: Given the folder structure explained, try adding to the v2 .htaccess at the beginning the following:

RewriteBase /

You still haven't explained if you can or cannot use RewriteLog (I presume you can't)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文