Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to force www addition or removal #1380

Closed
yajo opened this issue Apr 4, 2017 · 8 comments
Closed

Add option to force www addition or removal #1380

yajo opened this issue Apr 4, 2017 · 8 comments
Milestone

Comments

@yajo
Copy link

yajo commented Apr 4, 2017

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 Allow multiple rules from docker labels containers with traefik.<servicename>.* properties #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.
@timoreimann
Copy link
Contributor

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

@yajo
Copy link
Author

yajo commented Apr 7, 2017

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

@timoreimann
Copy link
Contributor

@yajo true, you're right.

@timoreimann timoreimann added the kind/enhancement a new or improved feature. label Apr 7, 2017
yajo added a commit to Tecnativa/doodba that referenced this issue Apr 10, 2017
- Do not add the `X-Forwarded-*` headers because the previous Traefik
  proxy should be adding them, and we don't want to override them.
- By default, as a good SEO rule of thumb, we only want to expose Odoo
  in a single host name, forcing all other hosts to get redirected to
  the main one, so we add the `FORCEHOST` support for the middle proxy.

We won't need this after traefik/traefik#1380 is fixed and released.
@ldez
Copy link
Contributor

ldez commented Apr 22, 2017

Related to #919

@ldez ldez added the priority/P3 maybe label Jun 12, 2017
@13rac1
Copy link

13rac1 commented Oct 15, 2017

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

@yajo
Copy link
Author

yajo commented Oct 15, 2017

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
Copy link

aek commented Nov 29, 2017

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.

@traefiker
Copy link
Contributor

Closed by #2570.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants