apache不支持用mod_rewrite可靠实现静态资源时段访问控制,必须使用mod_authz_core+mod_authz_host的require expr指令,在授权阶段基于服务器本地时区的时间变量(如time_wday、time_hour)执行布尔判断,并用包裹require all denied和require expr规则。

Apache 本身不支持直接对静态资源做“仅在某时段开放”的访问控制,不能靠 mod_rewrite 实现可靠的时间拦截。真正可行、稳定的方式是使用 mod_authz_core 和 mod_authz_host 提供的 Require expr 指令,在授权阶段执行时间判断——它在请求处理流程中位置靠前、不可绕过,且不受缓存、代理或 HTTPS 终止影响。
确认模块已启用
确保以下两行在 Apache 主配置(如 httpd.conf 或 apache2.conf)中已加载且未被注释:
LoadModule authz_core_module modules/mod_authz_core.soLoadModule authz_host_module modules/mod_authz_host.so
大多数 Apache 2.4+ 发行版默认启用,可通过 apachectl -M | grep authz 验证。
写准时间表达式(关键细节)
Apache 时间变量返回整数,且按服务器本地时区解析(不是 UTC,也不是浏览器时区),常见变量有:
Apache Superset 是一个广泛采用的开源 BI 平台,用于 SQL 探索、图表构建和仪表板交付。当代理需要查询仓库数据、组装仪表板或使用成熟的分析界面解释指标而不是临时笔记本代码时,此技能非常有用。
-
time_wday:星期几,周一为 1,周日为 0 -
time_hour:小时(0–23),无补零 -
time_min:分钟(0–59),无补零
例如,“仅允许工作日 8:30–17:30 访问 /static/report.pdf”需拆解为布尔逻辑:
配置到具体资源路径
静态资源通常通过 <location></location> 或 <filesmatch></filesmatch> 定位。推荐用 <location></location> 精确匹配 URL 路径:
<location><requireall>
Require all denied
Require expr "time_wday >= 1 && time_wday = 8 && (time_hour
</requireall></location>
注意:<requireall></requireall> 必须包裹,否则 Require all denied 会被后续 Require expr 覆盖;不能省略 Require all denied,否则默认放行。
验证与调试建议
上线前务必测试:
- 先用宽松条件验证语法,比如
Require expr "time_hour == 16"(当前是 16 点),看是否能即时生效 - 检查服务器系统时区:
timedatectl,确保与业务预期一致 - 避免在 .htaccess 中使用该功能(部分环境不支持
Require expr在分布式配置中生效) - 重启 Apache 后,用
curl -I http://yoursite/static/report.pdf查看响应状态码:时段外应返回403 Forbidden









