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

.htaccess deny/allow specific folder within my current rules

· 52CE 我爱测

I am using a .htaccess rule similar to the below: # BEGIN Disable access to all files EXCEPT Order deny,allow Deny from all # Files <Files ~ ".(doc|xlsx|xlsm|xls|pdf|docx|txt|rtf|rdf|odf)$"> Allow from all </Files> # Page Content…

问题原文

原帖链接 · Ryflex · CC BY-SA 3.0

I am using a .htaccess rule similar to the below:

# BEGIN Disable access to all files EXCEPT
Order deny,allow
Deny from all

# Files
<Files ~ ".(doc|xlsx|xlsm|xls|pdf|docx|txt|rtf|rdf|odf)$">
    Allow from all
</Files>
# Page Content
<Files ~ ".(css|js)$">
    Allow from all
</Files>
# Images
<Files ~ ".(jpe?g|png|gif|bmp|cur|ico|webp)$">
    Allow from all
</Files>
# Fonts
<Files ~ ".(svgz?|eot|otf|tt[cf]|woff2?)$">
    Allow from all
</Files>

However, I also want to allow one specific .php file, I don't want to have to put a .htaccess rule in the folder where the specific .php file exists so I've been trying to do it with FilesMatch

Here are some examples:

<FilesMatch "\/folder1\/folder2\/folder3\/folder4\/\.(php)">
    Allow from all
    Satisfy Any   
</FilesMatch>

<FilesMatch "\folder1\folder2\folder3\folder4\file1.php">
    Allow from all
    Satisfy Any   
</FilesMatch>

I've even tried them with /home/hostaccount/public_html/ at the start of the location and also just /public_html/ at the start of the location.

I've tried adding ^ to the start of the location and finished it off with $ at the end.

Anyone know what I am doing wrong and more importantly how to fix it?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

There's not much difference between <Files ~ "..."> or <FilesMatch "..."> (or <Files "..."> in this context) since they all just match against the file basename, excluding the directory path.

So, if the files basename is unique within this directory tree then you can simply do:

<Files "file-to-allow.php">
    Allow from all
</Files>

However, this will match file-to-allow.php anywhere within this directory tree, if the same file basename occurs in multiple directories.

If you needed to match the full filesystem path (or strictly speaking the URL-path), without using a .htaccess file in the target subdirectory and without access to the server config (which would allow you to use a <Directory> container), then you could perhaps do something like the following, with the help of mod_setenvif:

SetEnvIf Request_URI "^/path/to/file-to-allow\.php$" letmein

Allow from env=letmein

The SetEnvIf directive sets the environment variable letmein if the Request_URI matches the specific URL (regex). This is then used in the following Allow directive to only allow if the environment variable is set.

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

资料来源与适用范围

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

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

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