首页 > 解决方案 > 使用带有 Traefik 的请求变量将“WWW”重定向到“Non-WWW”?

问题描述

对于我对 Traefik 的新手理解,我提前道歉,但是有没有办法使用基于请求的变量重写为“非 www”域?

我已经在谷歌上搜索了一个多小时,但找不到答案。

这是我如何在 Apache 中执行此操作的示例。您可以看到它正在使用HTTP_HOST变量:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

我可以看到我可以像这样设置标签:

--label "traefik.frontend.redirect.permanent=true" 
--label "traefik.frontend.redirect.regex=^https?://www.example.com/(.*)"
--label "traefik.frontend.redirect.replacement=https://example.com/${1}"

但这需要我example.com为所有事情做好准备。无论域如何,我都希望这个重定向。谢谢!

标签: traefik

解决方案


我在 Traefik Slack 小组中找到了一些帮助,一位用户为我指出了正确的方向。使用他们的文档(https://docs.traefik.io/configuration/entrypoints/#rewriting-url),他能够帮助我编写正则表达式来解决我的需求:

[entryPoints.https.redirect]
permanent=true
regex = "^https://www.(.*)"
replacement = "https://$1"

推荐阅读