Apache HTTPD – 递归拒绝特定文件扩展名和/或子目录?

huangapple go评论61阅读模式
英文:

Apache HTTPD - recursively deny specific file extensions and/or subdirs?

问题

在Apache 2.4的htdocs文件夹中,有一种复杂的目录和子目录结构。

是否可以创建一个单个的顶层.htaccess文件,以拒绝特定文件扩展名(例如.bak)和/或禁止进入特定目录名称(例如.git)整个子目录结构?

例如,当请求以下文件时,我想用HTTP 403 Forbidden作出响应:

htdocs/index.html.bak
htdocs/aaa/bbb/ccc/login.php.bak
htdocs/foooooooo/.git/subdir/some_pic.png

如果无法通过.htaccess实现,也许可以通过httpd.conf实现吗?

谢谢!

英文:

I have an Apache 2.4 web server, and in its htdocs folder, there is a complicated structure of directories and subdirectories.

Is it possible to create a single, top-level .htaccess file that would deny specific file extensions (e.g. .bak)
and/or traversing into specific directory names (e.g. .git)
for the whole subdirectory structure?

E.g., I would like to respond with HTTP 403 Forbidden when requested:

htdocs/index.html.bak
htdocs/aaa/bbb/ccc/login.php.bak
htdocs/foooooooo/.git/subdir/some_pic.png

If not via .htaccess, then perhaps this can be achieved via httpd.conf?

Thanks!

答案1

得分: 1

感谢!我最终得到了以下的.htaccess配置:

RewriteEngine On
RewriteRule \.bak$ - [F]
RewriteRule (^|/)\.git($|/) - [F]

当然,在httpd.conf中,这一行必须首先取消注释:

LoadModule rewrite_module modules/mod_rewrite.so
英文:

Thanks! I ended up with the following .htaccess:

RewriteEngine On
RewriteRule \.bak$          - [F]
RewriteRule (^|/)\.git($|/) - [F]

Of course, this line in httpd.conf has to be uncommented first:

LoadModule rewrite_module modules/mod_rewrite.so

huangapple
  • 本文由 发表于 2023年2月14日 20:05:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定