OPNsense - HAproxy How to Remove www. from Requests

The times changed and we are not used to type www. before an URL anymore. However, some automated systems or "old code" out there might still add www. in front of an URL.

To avoid such issues, we can use HAproxy to remove the www. from the request.

1) Create a Condition

HAProxy's configuration in OPNsense is modular. Let's first create a condition as below.

Services > HAProxy > Rules&Checks > Conditions

2) Create a Rule

The condition has to be linked to a rule.

Services > HAProxy > Rules&Checks > Rules

Set the parameter HTTP Redirect with the below statement.

prefix https://%[hdr(host),regsub(^www\.,,i)] code 301

3) Configure Public Services

My HAProxy has a shared front-end. OPNsense defines front-ends as Public Services and they are usually used to deal with the requests coming to the firewall.

Under the Rules section of the SHARED-FRONTEND add the REMOVE-WWW-RULE as below.

Services > HAProxy > Virtual Services > Public Services

4) Conclusion

The above configuration will strip www. from requests. However, when testing the configuration syntax OPNsense complained.

I believe that this is a bug on how the HAProxy package creates the configuration.

Resources

My Old pfSense configuration.

https://www.guide4moms.com/2021/12/110-unreal-the-matrix-resurrections-quotes.html