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

Block directory access with .htaccess rules does not work

· 52CE 我爱测

I am trying to block access to a specific folder and all of its content. I use methods suggested on the Internet, but they do not work. This method does nothing: RewriteRule ^folder_name/?$ - [F,L] ErrorDocument 403 default And t…

问题原文

原帖链接 · Roman Simonyan · CC BY-SA 4.0

I am trying to block access to a specific folder and all of its content. I use methods suggested on the Internet, but they do not work.

This method does nothing:

RewriteRule ^folder_name/?$ - [F,L]
ErrorDocument 403 default

And this one brings internal server error:

<Directory "/folder_name/">
deny from all
</Directory>

What is wrong here?

My htaccess file looks like this:

RewriteEngine on
Options -Indexes
DirectoryIndex index.html

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

#rewrite rules for base URLs
RewriteRule ^([^/\.]+)[/]*$ $1\.html [QSA,L]

社区高分解答

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

RewriteRule ^folder_name/?$ - [F,L]

This blocks access to the folder only; not its contents (because of the trailing end-of-string anchor). And since you have existing mod_rewrite directives, it will need to go near the top, before the existing directives.

Generally, you should have blocking directives first.

RewriteRule ^folder_name - [F]

: remaining directives follow

Alternatively, you create another .htaccess file inside the directory you want to block, with a single directive:

Require all denied

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

资料来源与适用范围

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

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

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