Strip www. from url in HAProxy

Started by Daves_nt_here, August 06, 2024, 04:01:09 AM

Previous topic - Next topic
I've setup a condition for host starting with "www."
Adding a custom rule with the following
http-request redirect prefix http://%[hdr(host),regsub(^www\.,,i)] code 301 if { hdr_beg(host) -i www. }

Give an error:
[ALERT] (32645) : config : parsing [/usr/local/etc/haproxy.conf.staging:110] : error detected in frontend 'Strip-www' while parsing 'http-request redirect' rule : error in condition: no such ACL : 'if'.

Where do I enter the rule syntax (found on https://www.haproxy.com/documentation/haproxy-configuration-manual/2-8r1/) or how do I go about this?

Set up an ACL for hdr_beg(host) -i www and do not mess with custom code.

How do I go about doing this from the GUI? Or is this something I need to do from the a ssh terminal?

August 16, 2024, 09:39:33 PM #3 Last Edit: August 16, 2024, 10:03:12 PM by doktornotor
In Services - HAproxy - Settings, from Rules & Checks dropdown select Conditions, use + to add the ACL. After that, under Rules, use that Condition to set up the redirect. In Execute function, select the http-request-redirect. Adjust the parameters as needed.

No idea why are you creating custom rules for something already available in the GUI.

Do NOT use any pass-through stuff unless absolutely required. Chances are high the ordering will not be correct, the syntax will not be correct and you will break things altogether.

Reading the Config Export would help with understanding how "conditions" (ACLs) are used. You created a condition, but seems like you are not using it at all in the custom rule, pasting wrong things to Option pass-through field.

Didn't really want to use the pass through or modify from ssh, but that's what googling for hours has led me to.

I have already tried with no luck:
Condition> Host contains: www.example.com
Rule> if selected conditions, http-request redirect - https://example.com



:) that is the rule, we know where they are created.
I mean where have you placed it to be used? Normally that would be on a public service.

Sorry, still trying to wrap my head around OPNsense, migrateing from Untangle.
I guess that is what I'm missing (Public Service) but this wont apply "HAProxy configtest found critical errors"

yes you were missing this. You create a condition and a rule, you did that.
Then you need to use the rule somewhere. That looks a normal place to use it. So that is not the reason for the config being wrong, something else is. I can't see it from those screenshots.
You are binding that service to ports 80 and 443 on all interfaces. Have you moved the GUI away from those? Maybe the config checks for that? And you have created a backend and a real server for it too, right?
In short, you've fixed what you were asking for, now back to basics.

Yes, I had changed my GUI port.
Been playing around with the rules since I sent my last message.
Figured it out by trial and error. I was looking at the rule for redirecting http > https and it has a "scheme" in the HTTP Redirect so I added it to my rule. "scheme https://example.com"
No backend or real server needed.

For anyone in the future reading this, here is a summery of what I did to strip the www from the domain name.


  • Condition
    Name: StripWWW
    Condition type: Host starts with
    Host Prefix: www.example.ca
  • Rule
    Name: StripWWW
    Select Conditions: StripWWW
    Execute functions: http-request redirect
    HTTP Redirect: scheme https://example.com
  • Public Services
    Name: StripWWW
    Listen Addresses: 0.0.0.0:443 0.0.0.0:80
    Select Rules: StripWWW