大网数据广告活动
服务器运维问答

How can I apply HSTS header to all vhosts over 443 in Apache

· 52CE 我爱测

I'm looking to add the HSTS header in Apache... # HSTS / Header Strict Transport Security Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" ... but I have a long list of vhosts for differe…

问题原文

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

I'm looking to add the HSTS header in Apache...

# HSTS / Header Strict Transport Security
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"

... but I have a long list of vhosts for different but related sites/sub-sites. I'd rather not have to define it in every one of my vhost definitions, but I'm not aware of a way to include a setting in the main https.conf that ONLY applies to the 443 / https versions of those vhosts since it raises warnings in validators when you apply HSTS to a standard 80 / http site.

I've tried wrapping it in <IfModule mod_ssl.c>...</IfModule> tags but if I'm not mistaken, this is really just asking Is the SSL module loaded? I tried searching lots of different ways, but when you don't know the term your looking for, it's difficult to sort through all the static. Any suggestions? Thanks!

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 4.0

You can do this with the <If> Directive and the Expressions available; request related variables:

  • REQUEST_SCHEME: The scheme part of the request's URI

    <If "%{REQUEST_SCHEME} == 'https'">
      Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
    </If>
    
  • HTTPS: on if the request uses https, off otherwise

    <If "%{HTTPS} == 'on'">
      Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
    </If>
    

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

资料来源与适用范围

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

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

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