Redirect non-existing site

Hi there,

I'm trying to create a redirection to my traefik-docker.
I have an API at https://domain.com/api and I would like to redirect all other to https://anotherdomain.com.
For example, if I hit https://domain.com, or https://domain.com/not-an-api, Traefik would redirect it.
How can I do that? I tried to search for these redirections, via middlewares, but I couldnt find the right answer.

Thanks!

A router with a rule Host(`domain.com`) && PathPrefix(`/`) and a low Priority.
and use a redirectRegex middleware to match everything .+ and redirect to where you want.

1 Like

Thanks for the reply cakiwi!

I tried Your way, but I'm not sure what I'm doing wrong.
This is the labels section for my container where the redirection needed:

labels:
      - "traefik.enable=true"
      - "traefik.http.routers.test.rule=Host(`domain.com`) && PathPrefix(`/api`)"
      - "traefik.http.routers.test.entrypoints=websecure"
      - "traefik.http.routers.test.tls.certresolver=myresolver"
      - "traefik.http.routers.test-redirect.priority=20"
      - "traefik.http.middlewares.redirectscheme.redirectscheme.scheme=https"
      - "traefik.http.routers.test-redirect.rule=Host(`domain.com`) && PathPrefix(`/`)"
      - "traefik.http.routers.test-redirect.priority=10"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://domain.com/(.*)"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://anotherdomain.com/"

It only throws back 404 error, and it wont redirect me.
Thanks for helping me out!

Your middleware is not attached to the router, so the request will be going to the container.

      - "traefik.http.routers.test-redirect.middlewares=test-redirectregex"
1 Like

Oh, well I forgot to add it to my router, my bad. :sweat_smile:
Now I added it but the same thing happens, no redirection, just the 404 error :frowning_face:

You also have two competing labels, but I do not think that will matter in this case:

traefik.http.routers.test-redirect.priority=10
traefik.http.routers.test-redirect.priority=20

First check traefik logs for errors and warnings.
Then check the traefik dashboard to make sure it all looks good.
My advice then would be to enable accesslog and use accesslog.format=json format option too. Try some more requests and make sure the requests are using the expected router.

1 Like

Thanks for helping me cakiwi!
I have this configuration now, I fixed the competing labels so now this is the Traefik config for my container:

 labels:
      - "traefik.enable=true"
      - "traefik.http.routers.test.rule=Host(`domain.com`) && PathPrefix(`/api`)"
      - "traefik.http.routers.test.entrypoints=websecure"
      - "traefik.http.routers.test.tls.certresolver=myresolver"
      - "traefik.http.routers.test.priority=20"
      - "traefik.http.middlewares.redirectscheme.redirectscheme.scheme=https"
      - "traefik.http.routers.test-redirect.rule=Host(`domain.com`) && PathPrefix(`/`)"
      - "traefik.http.routers.test-redirect.priority=10"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://domain.com/(.*)"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://anotherdomain.com/"
      - "traefik.http.routers.test-redirect.middlewares=test-redirectregex"

Its still not working. As I can see in the debug log, nothing catches my request. I have no warnings or errors. In the Traefik dashboard, I can see that this router exists, and the middleware connected to this router, so I cant see why it is not working properly. :frowning_face:

Can you share an example from the accesslog?

1 Like

This is the debug log:

time="2021-10-26T14:38:31Z" level=debug msg="Creating middleware" entryPointName=web middlewareType=RedirectRegex middlewareName=test-redirectregex@docker routerName=test-redirect@docker
time="2021-10-26T14:38:31Z" level=debug msg="Setting up redirection from ^https://domain.com/(.*) to https://anotherdomain.com/" routerName=test-redirect@docker entryPointName=web middlewareType=RedirectRegex middlewareName=test-redirectregex@docker
time="2021-10-26T14:38:31Z" level=debug msg="Adding tracing to middleware" entryPointName=web routerName=test-redirect@docker middlewareName=test-redirectregex@docker

And this is the access log when I try to reach https://domain.com/

{"ClientAddr":"-:6045","ClientHost":"-","ClientPort":"6045","ClientUsername":"-","DownstreamContentSize":19,"DownstreamStatus":404,"Duration":97410,"OriginContentSize":19,"OriginDuration":12602,"OriginStatus":404,"Overhead":84808,"RequestAddr":"domain.com","RequestContentSize":0,"RequestCount":1,"RequestHost":"domain.com","RequestMethod":"GET","RequestPath":"/","RequestPort":"-","RequestProtocol":"HTTP/2.0","RequestScheme":"https","RetryAttempts":0,"StartLocal":"2021-10-26T14:51:30.526965969Z","StartUTC":"2021-10-26T14:51:30.526965969Z","entryPointName":"websecure","level":"info","msg":"","time":"2021-10-26T14:51:30Z"}

And this is the dashboard:

No TLS configured for the router.

If you're only going to use TLS I recommend you enable TLS at the entrypoint level. Otherwise add the TLS configuration for this router too. I missed that when looking at your labels.

2 Likes

You were right. I configured the TLS too, and now its working fine. Thank You very much for all the help!

1 Like

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