Traefik 2.2 Domain Redirection

Hello There!

I am using Traefik 2.2 with Docker and i don't understand how its possible to redirect for example test.com to google.com.

This is my docker-compose.yml file:

version: '3.5'

networks:
  traefik:
    name: traefik

  monitoring:
    external:
      name: monitoring

services:
  traefik:
    image: traefik:2.2
    restart: always
    container_name: traefik
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./acme.json:/acme.json
    ports:
      - 80:80
      - 443:443
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.rule=Host(`traefik.domain`)"
      - "traefik.http.routers.traefik.entrypoints=web-secure"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.middlewares=auth"
      - "traefik.http.middlewares.auth.basicauth.users=admin:password"
      #Password generieren: echo $(htpasswd -nb user password) | sed -e s/\\$/\\$\\$/g
    networks:
      - traefik
      - monitoring

And this is my traefik.toml file:

# Network traffic will be entering our Docker network on the usual web ports
# (ie, 80 and 443), where Traefik will be listening.
[entryPoints]
  [entryPoints.web]
    address = ":80"

  [entryPoints.web-secure]
    address= ":443"
    [entryPoints.web-secure.http.tls]
      certResolver = "letsencrypt"

    #Redirection from HTTP to HTTPS
    [entryPoints.web.http]
      [entryPoints.web.http.redirections]
        [entryPoints.web.http.redirections.entryPoint]
        to = "web-secure"
        scheme = "https"

#Integration with Let's Encrypt
[certificatesResolvers.letsencrypt.acme]
  email = "mail@mail.com"
  storage = "acme.json"
  [certificatesResolvers.letsencrypt.acme.tlsChallenge]

#[log]
#  level = "DEBUG"

[api]
  #Defaul=true
  dashboard = true

# Enable retry sending request if network error
[retry]

# These options are for Traefik's integration with Docker.
[providers.docker]
  endpoint = "unix:///var/run/docker.sock"
  exposedByDefault = false
  network = "traefik"

I tried adding this to the TOML File but it just gave me 404 Error:

#Redirection of old projects to our homepage
[redirect.routers]
  [redirect.routers.test]
    rule = "Host(`test.com`)"
    middlewares = ["redirect-test"]

[redirect.middlewares]
  [redirect.middlewares.redirect-test.redirectRegex]
    regex = "^http://test.com/(.*)"
    replacement = "http://google.com/${1}"
    permanent = true

Thanks in Advance!

Same as another recent topic.

Hi cakiwi and thanks for your help!

So if I get this right i have to put the following code in my TOML file and then have to create a second file with the dynamic configuration?

[providers]
  [providers.file]
    filename = "/path/to/config/dynamic_conf.toml"
1 Like

So i managed to do the following dynamic configuration:

[http]
  [http.routers]
     [http.routers.redirection-router]
        rule = "Host(`test.com`)"
        #Defines the service of this router
        service = "redirection"
        #This Router listens to the entrypoint web and websecure (port 80 and 443)
        entrypoint=["web", "websecure"]
        #Defines the middleware of this router
        middlewares = ["redirection"]

     #This is where the actual redirection happens
     [http.middlewares]
       [http.middlewares.redirection.redirectRegex]
         regex = "^http://test.com/(.*)"
         replacement = "http://google.com/${1}"
         permanent = true

     [http.services]
       [http.services.redirection.loadbalancer]
         [[http.services.redirection.loadbalancer.servers]]
           url = "http://test.com:80"

But when i visit test.com Chrome tells me there are too many redirects

The Final Solution was that i first needed to add this lines into my static config:

#Redirection of old projects to our homepage (see ./redirection.toml for configuration)
[providers]
  [providers.file]
    filename = "/redirection.toml"

Afterwards i createt the file redirection.toml like this:

#This file is used to redirect old project websites (trinex.eu, secom20.eu) to our homepage.

[http.routers]
   [http.routers.redirection-router]
      rule = "Host(`test.com`)"
      #Defines the service of this router
      service = "dummy"
      #This Router listens to the entrypoint web and websecure (port 80 and 443)
      entrypoint=["websecure"]
      #Defines the middleware of this router
      middlewares = ["redirect"]


   [http.middlewares]
     [http.middlewares.redirect.redirectRegex]
       regex = "(.*)test.com(.*)"
       replacement = "http://google.com"
       permanent = true


   #This Service is completely unnecessary but it if you delete it things will go wrong
   [http.services]
     [http.services.dummy.loadbalancer]
       [[http.services.dummy.loadbalancer.servers]]
         url = "localhost"

And now it works perfectly. Hope this might help you if you stumble upon this.

It's pretty straightforward if you are using an Ingressroute. Let say I want to redirect from example.com to app.example.com using traefik

First thing would be to create a Middleware :

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: example-redirectregex
spec:
  redirectRegex:
    regex: ^https://example.com/(.*)
    replacement: https://app.example.com/${1}

If you already have an Ingressroute then you can add an additional rule that will point example.com to the same backend service used by app.example.com and use the middleware we created above

- kind: Rule
    match: Host(`example.com`)
    middlewares:
    - name: example-redirectregex
      namespace: traefik
    services:
    - kind: Service
      name: master-prod
      namespace: app-web
      port: 80

So, finally your Ingressroute should look something like the below:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  generation: 3
  labels:
    app: app-web
    product: example.com
  name: example-com
  namespace: traefik
spec:
  entryPoints:
  - websecure
  routes:
  - kind: Rule
    match: Host(`example.com`)
    middlewares:
    - name: example-redirectregex
      namespace: traefik
    services:
    - kind: Service
      name: example
      namespace: app-web
      port: 80
  - kind: Rule
    match: Host(`app.example.com`)
    services:
    - kind: Service
      name: example
      namespace: app-web
      port: 80
  tls:
    secretName: cert-example-com

Now, if you browse example.com it should redirect you to app.example.com

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.