【问题标题】:"WWW" to "Non-WWW" redirect using request variables with Traefik?使用带有 Traefik 的请求变量将“WWW”重定向到“Non-WWW”?
【发布时间】:2019-01-12 17:24:14
【问题描述】:

对于我对 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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这是适用于我的 docker 设置:

        service-a:
          labels:
            - "traefik.http.routers.service-a.rule=Host(`example.com`) || Host(`www.example.com`)"
            - "treafik.http.middlewares.service-a-redirect.redirectregex.regex=^(https?://)www.(.*)$$"
            - "traefik.http.middlewares.service-a-redirect.redirectregex.replacement=$${1}$${2}"
            - "traefik.http.routers.service-a.middlewares=service-a-redirect"
      

      它同时支持httphttps

      灵感来自:https://medium.com/better-programming/traefik-2-1-as-a-reverse-proxy-c9e274da0a32

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-09
        • 2013-09-03
        • 1970-01-01
        • 2019-10-26
        • 2018-07-03
        • 2016-10-14
        • 2022-08-03
        相关资源
        最近更新 更多