I’m currently using Haproxy to balance several express.js nodes. I know that it’s possible to redirect using express.js, but I was hoping to do so with Haproxy.
I was wondering how I can do a permanent redirect from http://www.mysite.com to mysite.com?
Please see the documentation of the redirect prefix rule for more information.
If you are using a newer version of HAProxy, i.e. at least 1.6, you can use a more generic syntax which allows to redirect any host, not just explicitly named
Here, we are using the regsub filter to dynamically generate the correct hostname without the
www.prefix.In case you want to perform a redirect the other way around, i.e. to add a
wwwif there is none already, the rule becomes simpler: