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

如何在.htaccess文件中为多个文件设置密码保护?

解决.htaccess多文件密码保护失效问题

你的配置失效的核心原因是每个<Files>指令都没有对应的</Files>闭合标签,Apache无法正确解析这种不完整的区块结构,导致密码保护规则无法生效。

下面提供两种正确的配置方案:

方案一:补全闭合标签的独立<Files>块

<Files "startside-int.html">
    AuthType Basic
    AuthName "Password Protected Area"
    AuthUserFile "/customers/a/1/e/korevi.no/httpd.www/.htpasswd"
    Require valid-user
</Files>

<Files "referater.html">
    AuthType Basic
    AuthName "Password Protected Area"
    AuthUserFile "/customers/a/1/e/korevi.no/httpd.www/.htpasswd"
    Require valid-user
</Files>

方案二:用<FilesMatch>批量匹配(更简洁)

通过正则表达式一次性匹配多个目标文件,减少重复代码:

<FilesMatch "^(startside-int|referater)\.html$">
    AuthType Basic
    AuthName "Password Protected Area"
    AuthUserFile "/customers/a/1/e/korevi.no/httpd.www/.htpasswd"
    Require valid-user
</FilesMatch>

额外注意事项

  • 确认.htpasswd文件的路径绝对正确,且Apache进程拥有该文件的读取权限
  • 测试前清除浏览器缓存,避免旧缓存干扰验证结果
  • 若服务器配置允许,可重启Apache服务确保配置立即生效

内容的提问来源于stack exchange,提问作者Nils-Ove Nipen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 16:28:10