HAProxy community

Remove www. from all types of requests


Hi All, I have an SSL enabled domain.com, that strips away any www. prefix and redirects to https. Unfortunately this only works if a request is as follows:

if a user however tries https://www. it sends it over https but does not strip away the www.
I’m only having this issue with www. if the initial request is over https. I would really appreciate any help ideas on this.


Here is my code on haproxy 1.5 so far

#Frontend HTTP
frontend public_http
acl host_www hdr_beg(host) -i www.
reqirep ^Host:\ www.(.*)$ Host:\ \1 if host_www
redirect code 301 prefix / if host_www
redirect scheme https code 301 if { hdr(Host) -i abc.domain.com } !{ ssl_fc }
redirect scheme https code 301 if { hdr(Host) -i xyz.domain.com } !{ ssl_fc }

Sorry I had to split this cos haproxy.org in their infinite wisdom decided you can have more than 2 “links” in your post.


Do you want to send a redirect to the domain without www, or do you want to rewrite (the browser still shows the www domain)?

I assume your want the former? Something like this should do it:
redirect prefix https://mysite.com if { hdr(host) -i www.mysite.com }

Also see:


Hi, thank you for the reply. After much diging around and trial n error, I came up with this:
http-request add-header X-Host-Redirect yes if { hdr_beg(host) -i www. }
acl host_redirect hdr_cnt(X-Host-Redirect) eq 1
reqirep ^Host:\ www.(.*)$ Host:\ \1 if host_redirect
redirect code 301 scheme https if host_redirect

How to remove www from all requests so wildcard SSL for *.exmaple.com will work for www.client1.example.com?