Skip to content

Enable redirect www to non-www and vice-versa #12

Open

Description

@hgezim

Thanks for this awesome repo.

It would be really nice if we could pass an environment variable to indicate that we want a redirect rule to happen from www.domain.example to backend domain.example or vice-versa.

Activity

  1. pierreozoux commented on Sep 10, 2018

    @pierreozoux
    Member

    Yes, I know this part was never clear...
    There was never a clear concept from our users.
    If you have a good idea for that, please PR.

    Maybe:

    • default redirect naked domain to www
    • if there is a var REDIRECT_WWW, then do the other way around.

    What do you think?

    (I'll not have the time to implement it)

  2. hgezim commented on Sep 13, 2018

    @hgezim
    Author

    @pierreozoux,

    I'll need to check if the domain listed in VIRTUAL_HOSTS is an eTLD+1 before we can start adding www to it. This go library does that: https://godoc.org/golang.org/x/net/publicsuffix

    Thoughts on how I might get this into templates/haproxy.cfg.tmpl?

  3. pierreozoux commented on Sep 13, 2018

    @pierreozoux
    Member

    nope :/

  4. hgezim commented on Mar 18, 2019

    @hgezim
    Author

    Here's what I have working:

    VIRTUAL_HOSTS can be set to single, command separated domains, or domains with redirection like so: "commerceowl.com=>www.commerceowl.com".

    This way it's quite explicit and no magic happens. Here's the relevant code:

    {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
       {{ $redirect_parts := split $host "=>" }}
       {{ if (gt (len $redirect_parts) 1) }}
         {{ $host := index $redirect_parts 1 }}
         {{ $redirect_from := index $redirect_parts 0 }}
         redirect location https://{{ $host }} code 301 if !acme { hdr(host) -i {{ $redirect_from  }} }
         use_backend {{ $host}} if { hdr(host) -i {{ $host }} }
       {{else}}
         {{ $host := index $redirect_parts 0 }}
         use_backend {{ $host}} if { hdr(host) -i {{ $host }} }
       {{end}}
    {{end}}
    
    {{ range $host_array, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
    {{ range $host := split $host_array "=>" }}
    backend {{ $host }}
      option http-server-close
      cookie SERVERID insert nocache indirect
      {{ range $container := $containers }}
        {{ $networkLen := len $container.Networks }}
        {{ if eq $networkLen 1 }}
          {{ $network := index $container.Networks 0 }}
      server Server {{ $network.IP }}:80 cookie Server
        {{ else }}
          {{ range $network := $container.Networks }}
            {{ if eq $network.Name "lb_web" }}
      server Server {{ $network.IP }}:80 cookie Server
            {{end}}
          {{end}}
        {{end}}
      {{end}}
    {{end}}
    {{end}}
    

    I'd create a pull request but I realized I'm behind and don't understand what the REVERSE_PROXY_FOR stuff does.

  5. hgezim commented on Apr 9, 2019

    @hgezim
    Author

    @pierreozoux, thoughts on the above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions