You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 11:52:11