使用 Traefik 的请求变量将“WWW”重定向到“非 WWW”?

Jay*_*ers 3 traefik

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

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

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

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

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

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

但这需要我example.com为一切做好准备。我希望无论域如何都可以重定向。谢谢!

Jay*_*ers 5

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

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