Global redirect www to non-www with HTTPS redirection

Yes! It could! you do not need the middleware for redirecting from http to https, if you apply entry point redirection. But in your last example when you get 404 at https://www.foo.com you are already at https, that is the redirection has already happened, and there is no router rule to match.

1 Like

So, I'm afraid I'm misunderstanding the rule of HostRegexp or the role of wwwsecure-catchall.

The http -> https redirection is made at the entry point level, and it's great! So all routers have just to deal with https. In my initial idea, the router rule HostRegexp(`{host:(www\.).+}`) should catch urls like https://www.foo.com and then the router should pass them to the middleware wwwtohttps in order to remove the www and to pass forward the request https://foo.com. But this happens only if I specify the option wwwsecure-catchall.tls.

This solution didn't work for me. There are tons of answers but still there is no official document for this by Traefik.

I tried this on traefik.yml but didn't work too.

middlewares:
  nowww-redirectregex:
    redirectRegex:
      permanent: true
      regex: "^https://www.(.*)"
      replacement: "https://$1"

So, I'm afraid I'm misunderstanding the rule of HostRegexp or the role of wwwsecure-catchall .

As far as I understand we got your particular problem solved. What you are trying to do right now is experiment with things to understand how they work. This is great! However sometimes it means that one loses focus, since there is no longer a clear problem to solve. Not having a goal to work towards can be confusing.

The http -> https redirection is made at the entry point level, and it's great! So all routers have just to deal with https.

Yep.

In my initial idea, the router rule HostRegexp({host:(www.).+}) should catch urls like https://www.foo.com and then the router should pass them to the middleware wwwtohttps in order to remove the www and to pass forward the request https://foo.com.

Sounds plausible :wink:

But this happens only if I specify the option wwwsecure-catchall.tls .

I feel we are going in circles here. You already reported that and I already told you that 404, that you are observing, means that no rules match and indeed in this case with the only tls router with a matching rule in your configuration removed so you no longer have a tls router with a matching rule. Remember, I cannot see your configuration, I can only go by what you posted.

1 Like

Well it's not possible to create "official document" for each and every use case. As you rightly say, similar questions has been asked and answered here to people's satisfaction. If you cannot get it right, post what you are trying to achieve, your full configs, relevant logs, what you are observing and what are you expecting.

1 Like

Of course, you're right. But, I think http to https and www to non-www redirect are so basic topics. All people need to do this without an exception. Also, we spend a lot of time and try so many different scenario to achieve this and still couldn't figure out.

http to https redirect is common and I believe well covered. Just search the documentation site and these forums for examples. www to non-www is much less common. In fact, I personally cannot remember any other topic than this one on the subject. And as you can see it was helped :wink:

Can you please share the working yaml www to non-www example for the last time? I am still getting 404 when I come with www like https://www.domain.com.

Thank you for your time.

Let's do it the other way, post what you are trying to achieve, your full configs, relevant logs, what you are observing and what are you expecting.

This is not because I have your use case working and do not want to share, it's simply because I don't have a working configuration for your particular case. But I'm sure we can get to the bottom of any problems that you might have with your configuration and fix it.

Hello @zespri, excuse me for my late reply and thanx for your kind support during my crazy experiments :upside_down_face:

I think I got the point. The misconception I was facing lies in the usage of the tls option of the router. I was erroneously believing that when the redirection happens at the entrypoint level, you have no longer to deal with the tls option at the router level.

Actually, you have to explicitly create a tls router regardless of global redirections at the entry point level... and it makes perfectly sense!

1 Like

Hi @zespri I posted a new topic and shared my full Traefik configuration. traefik.yml, Traefik's docker-compose.yml and Container's docker-compose.yml

This config is working with www and non-www. It is not removing www. It only redirects http to https.

http://www.blueway.app or http://blueway.app
https://www.blueway.app
Final URL should be like this: https://blueway.app

Thank you very much for your time.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.