Issues with redirection to an other subdomain with RedirectRegex

Hi all,

this is my first post here :slight_smile:

I'm having trouble with redirection of my files in Traefik, and I'm hoping someone can help me out.

My task is to redirect

  • from: http(s)://(www.)openhdfpv.org/wp-content/openhd-images/BaseImages/*
  • to: https://data.openhdfpv.org/openhd-imagest/BaseImages/*

Here's what I'm running:

  • WordPress in Docker Compose with Traefik2.9
  • MinIO in a separate Docker Compose file
  • I'm using the following labels:
labels:
  traefik.enable: true
  traefik.http.routers.wordpress-app.entrypoints: web
  traefik.http.routers.wordpress-app.rule: Host(`www.openhdfpv.org`,`openhdfpv.org`)
  traefik.http.middlewares.wordpress-app-https-redirect.redirectscheme.scheme: https
  traefik.http.routers.wordpress-app.middlewares: wordpress-app-https-redirect,redirect-baseimages-to-data
  traefik.http.routers.wordpress-app-secure.entrypoints: websecure
  traefik.http.routers.wordpress-app-secure.rule: Host(`www.openhdfpv.org`,`openhdfpv.org`)
  traefik.http.routers.wordpress-app-secure.tls: true
  traefik.http.routers.wordpress-app-secure.tls.certresolver: myresolver
  traefik.http.routers.wordpress-app-secure.service: wordpress-app
  traefik.http.services.wordpress-app.loadbalancer.server.port: 80
  traefik.docker.network: proxy
  traefik.http.middlewares.redirect-baseimages-to-data.redirectregex.regex: ^https?://(www.)?openhdfpv\.org/wp-content/BaseImages/(.*)
  traefik.http.middlewares.redirect-baseimages-to-data.redirectregex.replacement: https://data.openhdfpv.org/openhd-images/BaseImages/$${2}
  traefik.http.middlewares.redirect-baseimages-to-data.redirectregex.permanent: true

The URL https://data.openhdfpv.org/openhd-images/BaseImages/hello.jpg works fine, but when I try to access https://openhdfpv.org/wp-content/BaseImages/hello.jpg, I get a 404 error.

I've tried a couple of things to fix this, but nothing seems to work. Here's what I've done so far:

  • Checked that the regular expression and replacement in the redirect-baseimages-to-data middleware are correct.
  • Changed the order of middlewares
  • Checked that the files are available on the target server at the correct location.

However, none of these solutions (that I remembered) have worked. I'm not sure what else to try. Does anyone have any ideas?

Thanks in advance for your help!

First, I would try using Host() with || (β€žorβ€œ):

Host(`example.com`) || Host(`www.example.com`)

Second, enable Traefik debug log to see what’s happening.

Thanks @bluepuma77

I went through the logs and found a relevant entry:

time="2023-02-18T00:49:03Z" level=debug msg="Creating middleware" routerName=wordpress-app@docker entryPointName=web middlewareName=redirect-baseimages-to-data@docker middlewareType=RedirectRegex
time="2023-02-18T00:49:03Z" level=debug msg="Setting up redirection from ^https?://(www.)?openhdfpv\\.org/wp-content/BaseImages/(.*) to https://data.openhdfpv.org/openhd-images/BaseImages/${2}" entryPointName=web middlewareName=redirect-baseimages-to-data@docker middlewareType=RedirectRegex routerName=wordpress-app@docker
time="2023-02-18T00:49:03Z" level=debug msg="Adding tracing to middleware" middlewareName=redirect-baseimages-to-data@docker entryPointName=web routerName=wordpress-app@docker

EDIT:
Changing the Host() labels to use || didn't fixed the issue for me.

Edit2:
Here is the access.log:

[18/Feb/2023:07:49:23 +0000] "GET /wp-content/BaseImages/hello.jpg HTTP/2.0" 404 87495 "-" "-" 12271 "wordpress-app-secure@docker" "http://<internal_wordpress_ip>:80" 865ms

Can you check the Wordpress logs, is Wordpress receiving requests (with wrong path)?

Good point, I haven't checked so far.

"GET /wp-content/BaseImages/hello.jpg HTTP/1.1" 404 57205 "https://community.traefik.io/" "<browser_stuff>"

Looks like it's going straight through.


EDIT:
If you want to try regex: regex101


EDIT2 IMPORTANT:

I had only ${2} there, but it's actually $${2}

Did you miss to assign the middleware?

  traefik.http.routers.wordpress-app-secure.middlewares=redirect-baseimages-to-data

If you want to use multiple middlewares on a router, use , to separate them.

1 Like

Tank you very much @bluepuma77 :blush::+1:
It's working now.

Had to assign the middleware to

traefik.http.routers.wordpress-app-secure.middlewares

and not to

traefik.http.routers.wordpress-app.middlewares

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