大网数据广告活动
服务器运维问答

.htaccess files directive only matching when I'm checking the same folder

· 52CE 我爱测

I've got a server set up where I want a specific PHP file to be readable via the browser, but I want access to be denied to the rest of the PHP files. So I put this .htaccess in the server root: <Files "*.php"> Require all denied…

问题原文

原帖链接 · John Doe · CC BY-SA 4.0

I've got a server set up where I want a specific PHP file to be readable via the browser, but I want access to be denied to the rest of the PHP files. So I put this .htaccess in the server root:

<Files "*.php">
    Require all denied
</Files>

Then, I put this in a subfolder (let's say the folder is subdir) to match specific files:

<Files "folder/specific.php">
   Require all granted
</Files>

But it doesn't work. It only works if I put this directly in subdir/folder/specific.php:

<Files "*.php">
   Require all granted
</Files>

Why does it happen like that? Even if I use:

Order Allow, Deny
Allow from all

The same thing happens.

已采纳解答

原帖链接 · Nikita Kipriyanov · CC BY-SA 4.0

<Files> directive is not expected to have any path components, only the base file name component. There should be no slashes in the directive's argument, and * doesn't match slashes (as it normally works in the system shell, by the way). The core module documentation states:

The directives given within this section will be applied to any object with a basename (last component of filename) matching the specified filename.

Obviously, the basename of a filesystem object (the thing after the last /) can not match anything that contains a slash.

As it is suggested in the page describing configuration sections, to match specific files in specific directories, you normally nest <Files> within the corresponding <Directory>:

<Directory "/var/web/dir1">
    <Files "private.html">
        Require all denied
    </Files>
</Directory>

This implies it is wrong to write this as a single <Files "/var/web/dir1/private.html>. In your case, I suspect the server should have been complaining somewhere in the error log that .htaccess has incorrect directive.

Since .htaccess can not contain <Directory> section and itself corresponds to the directory path it is placed into, this translates to adding your <Files> section into the .htaccess file placed in the same directory where the target files are located (or any of its parents), and not using any path within the directive. This is exactly what you are did when "it only works".

Note also, the .htaccess documentation begins with:

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

So, the only benefit they provide is ability to alter some configuration of the server without accessing its configuration. Everything else is possible; if you have that access (and you certainly have, else the question here would be off topic), you should convert your .haccess into in-server configuration and disable AllowOverride, removing any performance penalty it incurs. The details of how to do that provided on the same page, under Merging of .htaccess with the main configuration files sub-header.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2024-01-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。