为什么mod_rewrite忽略我的[L]标志? [英] Why does mod_rewrite ignore my [L] flag?

查看:111
本文介绍了为什么mod_rewrite忽略我的[L]标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的.htaccess文件.如果URL与静态文件匹配,它应该从assets文件夹传递静态文件.否则,所有内容都应重定向到index.php.

This is my .htaccess file. It should deliver static files from assets folder if the url matches them. Otherwise, everything should be redirected to index.php.

请注意,此处的网址不包含assets作为段.因此example.com/css/style.css指向assets/css/style.css.

Note that the url doesn't contain assets as segemnt here. So example.com/css/style.css directs to assets/css/style.css.

RewriteEngine on

# disable directory browsing
Options -Indexes

# static assets
RewriteCond %{DOCUMENT_ROOT}/assets/$1 -f
RewriteRule ^(.*)$ assets/$1 [L]

# other requests to index.php
RewriteRule !^asset/ index.php [L]

不幸的是,像example.com/assets/css/style.css这样的URL也会传递文件,因为对于该URL,我的规则均不适用,并且Apache的默认行为也适用于传递文件.

Unfortunately, urls like example.com/assets/css/style.css also deliver the file, since for that url none of my rules applies and Apache's default behavior is applied which delivers the file.

所以我尝试将最后一行更改为此.我认为这会起作用,因为上述规则中的[L]标志应停止执行资产网址并提供它们.

So I tried changing the last line to this. I thought that this would work since the [L] flag in the rule above should stop execution for asset urls and deliver them.

RewriteRule ^(.*)$ index.php [L]

相反,并非所有请求都重定向到index.php,即使是静态资产(例如example.com/css/style.css)也都被重定向.为什么该标志不会停止执行重写规则,然后由谁来解决我的问题?

Instead, not all requests are redirected to index.php, even static assets like example.com/css/style.css. Why does the flag not stop execution of rewrite rules and who to fix my problem then?

推荐答案

我在

如果您在.htaccess文件或以下文件中使用RewriteRule 部分,一定要有一些了解 规则的处理方式.简化形式是 规则已处理,重写的请求将交还给 URL解析引擎将执行其可能的操作.它可能是 在处理重写的请求时,.htaccess文件或 可能会再次遇到该节,因此可能会运行规则集 从头开始.最常见的情况是,如果 规则会导致重定向-内部或外部-导致 请求重新开始.

If you are using RewriteRule in either .htaccess files or in sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it. It is possible that as the rewritten request is handled, the .htaccess file or section may be encountered again, and thus the ruleset may be run again from the start. Most commonly this will happen if one of the rules causes a redirect - either internal or external - causing the request process to start over.

因此,如果您在以下位置使用RewriteRule指令,则这一点很重要 在其中一种情况下,您需要采取明确的步骤来避免规则 循环,而不是仅依靠[L]标志来终止执行 一系列规则,如下所示.

It is therefore important, if you are using RewriteRule directives in one of these contexts, that you take explicit steps to avoid rules looping, and not count solely on the [L] flag to terminate execution of a series of rules, as shown below.

替代标记[END]不仅可以用于终止 当前一轮的重写处理,但阻止任何后续的重写 从每个目录(htaccess)上下文中进行处理.这 不适用于外部重定向导致的新请求.

An alternative flag, [END], can be used to terminate not only the current round of rewrite processing but prevent any subsequent rewrite processing from occurring in per-directory (htaccess) context. This does not apply to new requests resulting from external redirects.

为解决我的问题,我将[L]标志更改为[END].

To fix my problem, I changed to [L] flags to [END].

RewriteEngine on

# disable directory browsing
Options -Indexes

# static assets
RewriteCond %{DOCUMENT_ROOT}/assets/$1 -f
RewriteRule ^(.*)$ assets/$1 [END]

# other requests to index.php
RewriteRule !^asset/ index.php [END]

这篇关于为什么mod_rewrite忽略我的[L]标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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