mod_rewrite:这个 RewriteRule 有什么作用? [英] mod_rewrite: what does this RewriteRule do?

查看:13
本文介绍了mod_rewrite:这个 RewriteRule 有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这些条件(我知道它们是什么意思/做什么):

RewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -d

第一规则有什么作用?那孤独的冲刺是为了什么?

RewriteRule ^.*$ - [NC,L]重写规则 ^.*$/index.php [NC,L]

我已经将它与 Zend 框架结合使用了很长一段时间,但我从来没有真正理解第一条规则的确切作用.

解决方案

RewriteCond 指令 仅描述了 RewriteRule 指令.所以 RewriteCond 必须始终与 RewriteRule 相关联.

在您的情况下,三个 RewriteCond 可能属于第一个 RewriteRule,如下所示:

RewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -d重写规则 ^.*$ - [NC,L]

现在,如果 RewriteRule 的模式与当前请求 URL 匹配(之前删除的每个目录路径)并且满足条件,则应用此规则.>

在这种情况下,只有在将请求 URL 映射到文件系统时,它匹配文件大小大于 0 的现有文件 (-s) 或符号链接 (-l) 或目录 (-d).因此,您的规则将应用于可以映射到文件系统中现有内容的任何 URL(^.*$ 匹配任何内容).替换 - 只是意味着不改变任何东西.而NC(不区分大小写,不区分大小写,在此上下文中无用)和L(如果应用了最后一条规则)是flags 修改规则的模式、替换或执行.

Given these conditions (I know what they mean/do):

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 

What does the first rule do? What is that lonely dash for?

RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

I've been using this for quite some time now in combination with the Zend Framework, but I never really got what the first rule does exactly.

解决方案

The RewriteCond directive just describes an additional condition for a RewriteRule directive. So RewriteCond must always be associated with a RewriteRule.

In your case the three RewriteCond probably belong to the first RewriteRule like this:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

Now this rule is applied if the pattern of the RewriteRule matches the current request URL (per-directory path stripped before) and if the condition is fulfilled.

In this case the condition is only true if when mapping the request URL to the filesystem it matches either an existing file with the file size greater than 0 (-s), or a symbolic link (-l) or a directory (-d). So your rule will be applied for any URL (^.*$ matches anything) that can be mapped to something existing in your filesystem. The substitution - just means to not change anything. And the NC (no case, case insensitive, useless in this context) and L (last rule if applied) are flags that modify either the pattern, replacement or the execution of the rule.

这篇关于mod_rewrite:这个 RewriteRule 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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