16

According to the Traefik 1.7 documentation you should be able to have Traefik perform a 302 redirect using:

  • traefik.ingress.kubernetes.io/redirect-regex
  • traefik.ingress.kubernetes.io/redirect-replacement

My goal is to simply remove the www. from the address.

This is what I've tried, but I get a 404 service not found.

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: www-redirect
  namespace: public
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/redirect-regex: ^https?://www.example.com/(.*)
    traefik.ingress.kubernetes.io/redirect-replacement: https://example.com/$1
spec:
  rules:
  - host: www.example.com

Unfortunately the documentation isn't explicit on how to use them. At the time of writing the only google hit on this is the documentation (above).

My current work around (assuming it'll help explain the question) is to route www. traffic to nginx which returns a 302.

server {
    listen       80;
    server_name  www.example.com;
    return 302 https://example.com$request_uri;
}

This seems like overkill.

vhs
  • 9,316
  • 3
  • 66
  • 70
DanielM
  • 6,380
  • 2
  • 38
  • 57

1 Answers1

6

I was having the same issue and ended up making it work with:

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: www-redirect
  namespace: public
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/preserve-host: "true"
    traefik.ingress.kubernetes.io/redirect-permanent: "true"
    traefik.ingress.kubernetes.io/redirect-regex: "^https://www.(.*)"
    traefik.ingress.kubernetes.io/redirect-replacement: "https://$1"
spec:
  tls:
    - hosts:
        - "example.com"
        - "www.example.com"
      secretName: example-tls
  rules:
  - host: example.com
  - host: www.example.com

Basically I needed both rules.

As a side note, I also start the trafik pod with the following flags:

args:
   - --api
   - --kubernetes
   - --logLevel=INFO
   - --entryPoints=Name:https Address::443 TLS
   - --entrypoints=Name:http Address::80 Redirect.EntryPoint:https
   - --defaultentrypoints=https,http
  • As an aside, we had a lot of trouble with the `--kubernetes` flag related to namespacing, highly recommend using a config file instead. – DanielM Dec 11 '18 at 10:12