Redirect rule problem

Trying to set up a redirect rule for a specific front end via consul catalog tags.

What I am trying to do is match: /path1/admin and redirect in that single case to /path1/admin/ (the backend can't handle the missing /)

My tags are the following:

traefik.frontends.cust1.redirect.permanent=false
traefik.frontends.cust1.redirect.regex=^(.*)/admin$
traefik.frontends.cust1.redirect.replacement=$1/admin/

Testing the regex and replacement in regex101 look good, I can see the redirect taking effect in the browser.

The problem I am hitting is a redirect loop. The request for /path1/admin gets a 301/302 back to /path1/admin NOT /path1/admin/

Any suggestions?

Traefik v1.7.11

@wilhelmi,

Can you please provide the results of:

curl -v yourdomain/path1/admin?

Would like to see the response.

domains and IP sanitized and I can't post text due to new user limitations.

Are you sure that Traefik is generating that redirect?

Golang camel-cases all headers, but none of those response headers are cased that way. There are also headers added that Traefik doesn't add for redirects....

And you say you have configured the redirect to be non-permanent...but a 301 is being returned.

Is it possible that your backend is returning the 301?

Traefik is behind a GCP load balancer, but hitting traefik directly yields the same problem. I did shut down the backend app to make sure it is out of the loop. So I don't think that is the problem.

The permanent is correct, I was flipping that setting to make sure the redirect was traefik. Here is what I am seeing:

These 2 tags seem to work as expected:

"traefik.frontends.cust1.redirect.permanent=true",
"traefik.frontends.cust1.redirect.regex=(.*)/admin$",

The replacement tag has me stumped.
traefik.frontends.cust1.redirect.replacement=$1/admin/ gives me the redirect loop.

Changing it traefik.frontends.cust1.redirect.replacement=$1/admin-test/ still gives the same redirect loop. The -test isn't added.

I updated to 1.7.13 to make sure there wasn't a bug fix I was missing, same result.

@wilhelmi,

Can you check if the redirect is properly being loaded by viewing the dashboard, or by enabling the api?

If so, can you share what is loaded? (feel free to sanitize any data. Just want to ensure that there are no issues with escaping, and that the redirect is being properly loaded).

Here is relevant config from the dashboard:

53%20PM

Hello @wilhelmi,

That portion of the configuration seems to be correct.

Can you provide us the full configuration, and the configuration you use to run traefik?

Thanks!

Traefik v1.7.13 --configfile only parameter passed in systemd unit file

traefil.toml

InsecureSkipVerify = true

debug = true
logLevel = "DEBUG"

[traefikLog]
 filePath = "/var/log/traefik.log"
 format = "common"

[accessLog]
 filePath = "/var/log/traefik-access.log"

[api]
 dashboard = true

[ping]


[entryPoints]
[entryPoints.http]
  address = ":80"

[consulCatalog]
endpoint = "consul.internal:8500"
exposedByDefault = false
prefix = "traefik"
frontEndRule = "Host:test.com"

consul-service.json

{
  "service": {
    "name": "backend-service",
    "tags": [
      "traefik.protocol=https",
      "traefik.enable=true",
      "traefik.frontends.cust1.headers.customRequestHeaders=X-Forwarded-Url-Base:/path1||X-Forwarded-Request-Context:https://sample.com/path1",
	  "traefik.frontends.cust1.redirect.permanent=true",
	  "traefik.frontends.cust1.redirect.regex=(.*)/admin$",
	  "traefik.frontends.cust1.redirect.replacement=$1/admin-test/",
	  "traefik.frontends.cust1.entryPoints=http",
      "traefil.frontends.cust1.backend=backend-service",
      "traefik.frontends.cust1.rule=HostRegexp:{catchall:.*};PathPrefixStrip:/path1/;AddPrefix:/backend"
    ],
    "port": 6443
  }
}

Did some local debugging and have got it narrowed down to redirect.go line 95

if addPrefix, addPrefixOk := req.Context().Value(middlewares.AddPrefixKey).(string); addPrefixOk {
		if len(addPrefix) > 0 {
			parsedURL.Path = strings.Replace(parsedURL.Path, addPrefix, "", 1)
		}
	}

parsedURL.path gets set to the original request, path1/admin, hence the loop.

I did some experimenting with, ReplacePathRegex and the backend gets wonky. Looks like rule processing happens before redirect processing.

Is there a way with Traefik 1.x to setup redirect rules the get applied before rule processing?

1 Like

I have exactly the same problem but with Traefik 1.7.16 and a docker-compose set-up. I use a PathPrefixStrip rule and want to perform a redirect from "/" to a html file in a subdirectory. But whatever I enter as replacement value Traefik sends a redirect to "/" resulting in a redirect loop.

Download links:
docker-compose.yml
config.toml

Here is the solution folks!