Traefik Kubernetes 301 Redirect

1/1/2021

I have traefik installed via the helm chart.

I have a domain: example.com

It has some blog posts.

I now created a subdomain: subdomain.example.com

I have the list of my blogs urls:

/blog-1
/blog-2

Both the base domain and the subdomain are on the same cluster.

I want to have 301 redirects so that if someone tries to visit:

example.com/blog-1

they would be redirected to:

subdomain.example.com/blog-1

I do not want to direct with a wildcard just with my list of blog urls.

Thanks

Here is my middleware for redirect to https

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: https-only
  namespace: exmaple
spec:
  redirectScheme:
    scheme: https
    permanent: true

a redirect is:

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: test-redirectregex
spec:
  redirectRegex:
    regex: "https://example.com"
    replacement: "https://subdomain.example.com"
    permanent: true

can I have multiple redirectRegex in the same middleware? I would then just have lots of them to redirect each url

-- Henry
http-redirect
kubernetes
redirect
traefik

1 Answer

1/1/2021

Just one redirect per middleware, but you can have as many middleware as you want.

But in this case you can use the regex:

  redirectRegex:
    regex: "https://example.com/(blog-1|blog-2|whatever)"
    replacement: "https://subdomain.example.com/$1"
-- coderanger
Source: StackOverflow