Haproxy redirect www to non-www

断了今生、忘了曾经 提交于 2019-12-20 10:29:05

问题


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 www.mysite.com to mysite.com?


回答1:


redirect prefix http://example.com code 301 if { hdr(host) -i www.example.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

http-request redirect prefix http://%[hdr(host),regsub(^www\.,,i)] code 301 if { hdr_beg(host) -i www. }

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 www if there is none already, the rule becomes simpler:

http-request redirect prefix http://www.%[hdr(host)] code 301 unless { hdr_beg(host) -i www. }


来源:https://stackoverflow.com/questions/10401621/haproxy-redirect-www-to-non-www

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!