我找不到与此类似的问题,还有其他人提到了 https 重定向,但没有提到最小化重定向。
一直在寻找解决方案,但尚未解决。
我们为 WordPress 使用 Docker > Traefik,并将 www 作为 WordPress 的首选版本。有多个 WP 实例。域是动态添加的。
但是,使用此配置,我收到了两次重定向,从 http 到 https 到 https www
http://example.com/
https://example.com/
https://www.example.com/
有什么办法可以减少重定向?
理想情况下 301 重定向 从
http://example.com directly to https://www.example.com
Traefik 配置文件如下
defaultEntryPoints = ["http", "https"]
[web]
address = ":8080"
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
compress = true
[entryPoints.https.tls]
[acme]
email = "email@domain.com"
storage = "acme.json"
entryPoint = "https"
onDemand = false
OnHostRule = true
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "traefik.example.com"
watch = true
exposedbydefault = false
最佳答案
尝试更换您的 [entryPoints.http.redirect]
输入:
[entryPoints.http.redirect]
#entryPoint = "https"
regex = "^http:\/\/(www\.)*(example\.com)(.*)"
replacement = "https://www.$2$3"
permanent = true
Regex101
它不会处理
https://example.com/
条目,因此您需要添加:[entryPoints.https.redirect]
regex = "^https:\/\/(example\.com)(.*)"
replacement = "https://www.$1/$2"
permanent = true
如果你有多个frontedns,正则表达式会很难处理,所以你可以考虑使用a label on the container , 像这样:
traefik.frontend.headers.SSLRedirect=true
traefik.frontend.headers.SSLHost=www.example.com
从 1.7 开始,有新选项
SSLForceHost
这甚至会强制重定向现有的 SSL 连接。traefik.frontend.headers.SSLForceHost=true
关于https - traefik - HTTP 到 HTTPS WWW 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46085504/