“万维网"到“非 WWW"使用 Traefik 的请求变量重定向? [英] "WWW" to "Non-WWW" redirect using request variables with Traefik?
问题描述
对于我对 Traefik 的新手理解,我提前道歉,但是有没有办法使用基于请求的变量重写非 www"域?
I apologize in advance for my novice understanding with Traefik, but is there a way to rewrite to "non-www" domains with a request-based variable?
我已经在谷歌上搜索了一个多小时,但找不到答案.
I've been Googling for over an hour and cannot find an answer.
这是我如何在 Apache 中执行此操作的示例.您可以看到它正在使用 HTTP_HOST
变量:
Here is an example how I would do this in Apache. You can see it is using the HTTP_HOST
variable:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
我可以看到我可以像这样设置标签:
I can see that I can set labels like so:
--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
.我希望无论域如何都可以重定向.谢谢!
But this requires me to set example.com
for everything. I would like this to redirect regardless of the domain. Thanks!
推荐答案
我在 Traefik Slack 小组中找到了一些帮助,并且一位用户为我指明了正确的方向.使用他们的文档(https://docs.traefik.io/configuration/entrypoints/#rewriting-url),他能够帮我写正则表达式来解决我的需求:
I found some help in the Traefik Slack group and a user pointed me in the right direction. Using their documentation (https://docs.traefik.io/configuration/entrypoints/#rewriting-url), he was able to help me write regular expressions that solved my need:
[entryPoints.https.redirect]
permanent=true
regex = "^https://www.(.*)"
replacement = "https://$1"
这篇关于“万维网"到“非 WWW"使用 Traefik 的请求变量重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!