.htaccess规则无效:无法拦截composer.json与lib目录访问
.htaccess 配置方案:拦截composer.json并限制lib目录访问
一、拦截composer.json访问
你之前的规则未生效,大概率是因为Apache配置未开启.htaccess权限(需确保对应站点目录的AllowOverride All已设置),或者Apache版本匹配问题。以下是适配不同版本的有效规则:
Apache 2.4+ 版本规则
<Files "composer.json"> Require all denied </Files>
兼容Apache 2.2与2.4的规则
<Files "composer.json"> Order Allow,Deny Deny from all Require all denied </Files>
二、限制lib目录访问但允许文件访问
要实现「访问example.com/lib返回403,访问example.com/lib/example.php允许」,需借助mod_rewrite规则,配置如下:
RewriteEngine On # 仅拦截对lib目录的直接请求(目录本身或目录列表) RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^lib/ - [R=403,L]
完整.htaccess配置
将上述规则整合,最终的.htaccess文件内容为:
# 拦截composer.json访问 <Files "composer.json"> Require all denied </Files> # 限制lib目录直接访问,允许目录内文件访问 RewriteEngine On RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^lib/ - [R=403,L]
注意事项
- 确保Apache已启用
mod_rewrite模块 - 站点根目录的Apache配置中必须设置
AllowOverride All,否则.htaccess规则无法生效 - 若lib目录不在网站根目录,需调整
RewriteRule中的路径(比如^sub/lib/)
内容的提问来源于stack exchange,提问作者znaja
相关产品推荐
相关产品推荐

