traefik/traefik

Add option to force www addition or removal

yajo opened this issue · 8 comments

yajo commented

What version of Traefik are you using (traefik version)?

Version: v1.2.1
Codename: morbier
Go version: go1.7.5
Built: 2017-03-27_03:19:40PM
OS/Arch: linux/amd64

What is your environment & configuration (arguments, toml...)?

Docker, no swarm:

    command:
        - --ACME.ACMELogging
        - --ACME.Email=mail@example.com
        - --ACME.EntryPoint=https
        - --ACME.OnHostRule
        - --ACME.Storage=/etc/traefik/acme/acme.json
        - --DefaultEntryPoints=http,https
        - --EntryPoints=Name:http Address::80 Redirect.EntryPoint:https
        - --EntryPoints=Name:https Address::443 TLS
        - --LogLevel=INFO
        - --Docker
        - --Docker.EndPoint=http://dockersocket:2375
        - --Docker.ExposedByDefault=false
        - --Docker.Watch

What did you do?

I wanted to redirect www.example.com to example.com in a website running behind Traefik. I wanted to do the opposite (example.org to www.example.org) in another website.

What did you expect to see?

I expected frontends to let you have a list of alternative host names a main one that gets all of those redirected, so I could do something like this in my docker-compose.yaml:

labels:
  traefik.frontend.forcehost: example.com
  traefik.frontend.althosts: www.example.com,www2.example.com

ACME should get certs for all of them.

Given this situation of www. removal or addition is so common (I had to do that on almost every website I ever launched in my life), maybe a shortcut flag would be useful:

labels:
  traefik.frontend.www: force # or: remove

However, first option is more flexible indeed.

What did you see instead?

Traefik cannot redirect by frontend, just by entrypoint. This reminds me to #1346.

Possible workarounds

  1. Reconfigure Traefik itself with a spaghetti regexp on the frontends rule definition. This is quite unmaintainable because you'd be required to change it each time you boot a new microservice in a similar situation, and the regexp would grow and grow.
  2. Add a container to the stack that listens on www.example.com and just returns a 301 redirection to example.com. Do the same for example.org. This would be more maintainable than previous workaround, but it seems a little bit absurd.
  3. I guess #1257 will help in v1.13 on serving both sites from same container, although not on the redirection problem.
  4. Force the host name in your app. Depending on the app you're deploying, this can be easier or harder; in any case, still not a perfect solution.

Could the PathPrefixStrip and AddPrefix rules possibly help you achieve your goal?

yajo commented

I don't think so, we're talking about the host name, not the path.

@yajo true, you're right.

ldez commented

Related to #919

Related feature request #572 - closed with suggestion of Workaround#2.

yajo commented

I think they closed it because it was just a question, but this is a feature request and it's still open, so it's ok if I understood correctly. 🙄

aek commented

I have started to work on a solution to add a custom redirect feature per frontend, to be able to specify a virtual frontend and backend just for the matter of redirection from one domain to another one being able to specify the domain name as a regexp to match the domain name.
Using this kind of construction:

[backends]
    [backends.aek-http]
    [backends.aek]
        [backends.aek.servers.instance]
            url = "http://127.0.0.1:8069"

[frontends]
    [frontends.aek-http]
        redirectRegex = "(http://mio.aek)"
        redirectReplacement = "https://www.mio.aek"
        backend = "aek-http"
    [frontends.aek]
        backend = "aek"
        passHostHeader = true
        entrypoints = ["http"]
        [frontends.aek.routes.instance]
            rule = "Host:aek.localhost"

Also this allow to specify:

  • http to https
  • non-www to www
  • www to non-www
  • https to http
  • domain-x to domain-y (no matter what protocol is used http or https)

It's working with the redirection features, I could submit a pull request if this is the way it should work with traefik, perhaps there is a better way, but i'm new to traefik and golang and this is the better way that I figure out from the traefik source code.

Closed by #2570.