为什么RewriteCond%{REQUEST_FILENAME}!-d是必需的? [英] Why is RewriteCond %{REQUEST_FILENAME} !-d mandatory?
问题描述
访问 example.com/mywebsite /
.htaccess
起作用b RewriteEngine On
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^( 。*)$ index.php
而失败(404):
RewriteEngine On
RewriteRule ^(。*)$ index.php
我认为第二个解决方案也应该起作用。
解释这种行为,我们需要对您的文件系统做出一些假设,通过工作,您的意思是文件已经送达(您没有看到目录清单)...
.htaccess
文件位于文档根目录中,而 / mywebsite
是包含以下内容的物理目录: index.php
文件(或某些 DirectoryIndex
文档)。文档根目录中没有 index.php
文件。换句话说:
example.com/
.htaccess
mywebsite /
索引。 php
在这种情况下,当您请求 example.com/mywebsite/
会发生以下情况:
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^(。*)$ index.php
/ mywebsite /
是物理目录,因此第一个条件失败并且 RewriteRule
未处理。
mod_dir然后搜索 DirectoryIndex
,找到 index.php
和 .htaccess
文件已重新处理。现在,它映射到物理文件,因此第二个条件失败,并且 RewriteRule
未处理。
结果是请求了 example.com/mywebsite/index.php
。就像根本没有 .htaccess
文件一样。
RewriteRule ^(。*)$ index.php
但是,在这种情况下,没有条件。 RewriteRule
得到无条件处理,并且内部重写对 example.com/index.php
(严格来说,它是< filesystem-path-to-document-root> /index.php
),因为这是 .htaccess的位置
文件已找到。
但是,文档根目录中没有 index.php
文件;因此是404。
为什么
RewriteCond%{REQUEST_FILENAME}!-d
是必需的?
是否强制性实际上取决于您的文件系统以及您要执行的操作。但是通常,您通常不希望前端控制器处理物理目录。
!- f
条件通常更重要,因为您通常不希望 front controller 处理物理文件。当您要从文件系统上的同一区域提供静态资源(例如CSS,JavaScript和图像)时,这是必需的。但是,如果您想通过前端控制器控制对某些物理文件(也许是下载部分)的访问,则可以忽略此指令。
Why does this .htaccess
work when accessing example.com/mywebsite/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
whereas this fails (404):
RewriteEngine On
RewriteRule ^(.*)$ index.php
I thought the second solution should work as well.
In order to explain this behaviour, we need to make some assumptions about your file system, and by "work" you mean that a file is served (you don't see a directory listing)...
The .htaccess
file is located in the document root and /mywebsite
is a physical directory that contains an index.php
file (or some DirectoryIndex
document). There is no index.php
file in the document root. In other words:
example.com/
.htaccess
mywebsite/
index.php
In this scenario, when you request example.com/mywebsite/
the following happens:
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php
/mywebsite/
is a physical directory, so the first condition fails and the RewriteRule
is not processed.
mod_dir then searches for a DirectoryIndex
, finds index.php
and the .htaccess
file is reprocessed. This now maps to a physical file, so the second condition fails and the RewriteRule
is not processed.
The net result is that example.com/mywebsite/index.php
gets requested. The same as if there was no .htaccess
file at all.
RewriteRule ^(.*)$ index.php
However, in this scenario, there are no conditions. The RewriteRule
gets processed unconditionally and internally rewrites the request to example.com/index.php
(strictly speaking it's <filesystem-path-to-document-root>/index.php
) since that is where the .htaccess
file is located.
However, there is no index.php
file in the document root; hence the 404.
Why is
RewriteCond %{REQUEST_FILENAME} !-d
mandatory?
Whether it is mandatory or not is really dependent on your filesystem and what you are trying to do. But generally, you don't normally want physical directories to be processed by the front controller.
The !-f
condition is usually more important since you often don't want physical files to be processed by the front controller. This is required when you want to serve static resources (eg. CSS, JavaScript and images) from the same area on the filesystem. However, you might omit this directive if you wanted to control access to some physical files (perhaps a "download" section) through the front controller.
这篇关于为什么RewriteCond%{REQUEST_FILENAME}!-d是必需的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!