URL redirections

Hello world,

I'm trying to implement a basic redirection in Traefik's file provider. I would
like the following:

http://example.com/foobar      ->  https://foobar.example.com
http://www.example.com/foobar  ->  https://foobar.example.com
https://example.com/foobar     ->  https://foobar.example.com
https://www.example.com/foobar ->  https://foobar.example.com

Here's my attempt:

http:
  middlewares:
    redirect-foobar:
      redirectRegex:
        permanent: true
        regex: "^https?://(www\\.)?example\\.com/foobar(/?.*)"
        replacement: "https://foobar.example.com${2}"
  routers:
    catch-foobar:
      middlewares:
        - redirect-foobar
      rule: Host(`example.com`) || Host(`www.example.com`)
      service: noop@internal

But when I try it out, here's what I get:

$ curl -I http://www.example.com/foobar 
HTTP/1.1 308 Permanent Redirect
Location: https://foobar.example.com
Date: Wed, 02 Mar 2022 02:12:48 GMT
Content-Length: 18
Content-Type: text/plain; charset=utf-8

# That's good =)
$ curl -I https://www.example.com/foobar
HTTP/2 404 
content-type: text/plain; charset=utf-8
x-content-type-options: nosniff
content-length: 19
date: Wed, 02 Mar 2022 02:05:22 GMT

# Does that mean the request didn't even make it to the catch-foobar router?

Topics such as this one helped a lot, but the https case remains problematic.

What am I doing wrong? Any help would be greatly appreciated.

Thanks,

C

By the looks of it your router doesn't have https enabled. You'll need to add a tls enabled entrypoint to this router.

I would recommend using entrypoint redirection if you're exclusively using https for your routes.

That solved it, thanks a lot! So my configuration now looks like this:

http:
  middlewares:
    redirect-foobar:
      redirectRegex:
        permanent: true
        regex: "^https?://(www\\.)?example\\.com/foobar(/?.*)"
        replacement: "https://foobar.example.com${2}"
  routers:
    catch-foobar:
      middlewares:
        - redirect-foobar
      rule: Host(`example.com`) || Host(`www.example.com`)
      service: noop@internal
      tls:
        certResolver: default

where default is a certificate resolver defined in my traefik.yml.

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