大网数据广告活动
网站运营与故障问答

.htaccess - How to properly disable the execution of scripts in a folder?

· 52CE 我爱测

I have this folder path: pulic_html/user/269851516/ in the folder /user/ i have many subfolders that is created when the user create a account, and they can upload avatars to this folder, i want to disable the execution of script…

问题原文

原帖链接 · Natalie · CC BY-SA 4.0

I have this folder path: pulic_html/user/269851516/in the folder /user/ i have many subfolders that is created when the user create a account, and they can upload avatars to this folder, i want to disable the execution of scripts in this folder, so i created a .htaccess and put in public_html/user/:

<Files *>
    SetHandler none
    SetHandler default-handler

    Options -ExecCGI
    php_flag engine off
    RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
</Files>

with this code inside my .htaccess nobody can execute scripts in this folder (i gues...). I have searched about it a bit, and i found different ways to do it, so i don't know if i did it correctly, and if there's something missing.

已采纳解答

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

with this code inside my .htaccess nobody can execute scripts in this folder (i gues...).

The only way to know whether it works "on your server" is to test it. If it works on your server to block direct script access (from the client) for the type of scripts that you are trying to block then "it works".

With the many different servers, there isn't necessarily a single/best way to do this. The directives you posted are arguably "generic" - designed to work across several different servers. You don't need all of them.

SetHandler none, SetHandler default-handler and RemoveHandler all do the same sort of thing so I can't imagine they are all required. It's likely that different server configs require different settings.

php_flag engine off will only work when PHP is installed as an Apache module. It won't work if PHP is running as CGI/FastCGI - in fact, you may even get a 500 internal server error!

The only change I would perhaps make is to change Options -ExecCGI to the more encompassing Options None. This also disables Includes (server-sides includes), Indexes (directory listings) etc. But it's possible these are already disabled in your server config.

The <Files> wrapper may be required on some servers if using the SetHandler/RemoveHandler method to block running scripts. Whilst <Files *> simply targets every file, as if there is no wrapper at all, it changes the order of execution so the directives run later - to ensure they override any earlier AddHandler directives.


UPDATED:

However, to clarify... this doesn't necessarily "disable the execution of scripts in a folder". It prevents the client (user-agent / browser) from making direct (HTTP) requests to scripts in a directory. Any script can still be executed in this directory if it is "called" (eg. included) from another script elsewhere on your system.

For example, say you have protected the /public_html/user directory tree with a .htaccess file at /public_html/user/.htaccess and you have another PHP script elsewhere on your filesystem, say at /public_html/foo/bar.php and bar.php has a statement like:

include($_SERVER['DOCUMENT_ROOT'].`/user/myavatar.jpg`);

Then myavatar.jpg will still be "executed", despite /user/.htaccess disabling script access to this directory. (In this contrived example, myavatar.jpg is not a JPG image, but a PHP script containing <?php blocks - the file extension is irrelevant). The .htaccess file is only processed for direct HTTP access, not filesystem access from other scripts.

See also this related question:

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

资料来源与适用范围

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

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

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