OPNsense Forum

English Forums => Web Proxy Filtering and Caching => Topic started by: Daves_nt_here on August 06, 2024, 04:01:09 AM

Title: Strip www. from url in HAProxy
Post by: Daves_nt_here on August 06, 2024, 04:01:09 AM
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/ (https://www.haproxy.com/documentation/haproxy-configuration-manual/2-8r1/)) or how do I go about this?
Title: Re: Strip www. from url in HAProxy
Post by: doktornotor on August 13, 2024, 11:30:58 AM
Set up an ACL for hdr_beg(host) -i www and do not mess with custom code.
Title: Re: Strip www. from url in HAProxy
Post by: Daves_nt_here on August 16, 2024, 09:31:39 PM
How do I go about doing this from the GUI? Or is this something I need to do from the a ssh terminal?
Title: Re: Strip www. from url in HAProxy
Post by: doktornotor on August 16, 2024, 09:39:33 PM
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.
Title: Re: Strip www. from url in HAProxy
Post by: Daves_nt_here on August 17, 2024, 10:20:20 PM
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
Title: Re: Strip www. from url in HAProxy
Post by: cookiemonster on August 17, 2024, 11:30:03 PM
where have you put this rule ?
Title: Re: Strip www. from url in HAProxy
Post by: Daves_nt_here on August 17, 2024, 11:41:21 PM
See pic.
Title: Re: Strip www. from url in HAProxy
Post by: cookiemonster on August 17, 2024, 11:42:50 PM
:) 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.
Title: Re: Strip www. from url in HAProxy
Post by: Daves_nt_here on August 18, 2024, 12:26:09 AM
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"
Title: Re: Strip www. from url in HAProxy
Post by: cookiemonster on August 18, 2024, 01:24:22 AM
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.
Title: Re: Strip www. from url in HAProxy
Post by: Daves_nt_here on August 18, 2024, 02:22:15 AM
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.
Title: Re: Strip www. from url in HAProxy
Post by: Daves_nt_here on August 18, 2024, 02:30:53 AM
For anyone in the future reading this, here is a summery of what I did to strip the www from the domain name.