Thanks for the help Jens! 

-- 

 
Tom Braarup          
Senior Linux Administrator
IT Infrastruktur Platform - Digital Udvikling og Teknologi
________________________________

 



-----Original Message-----
From: Jens Wahnes <[email protected]>
To: [email protected]
Subject: Re: How do I use "ltrim"?
Date: 08/25/2025 02:21:18 PM

Tom Braarup wrote:
> How do I use "ltrim" 
> (https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#7.3.1-ltrim)
>  ?
> 
> Trying to do a redirect short.com/123456 to example.com/id123456
> http-request redirect locationhttps://example.com/id%[path,ltrim(/)] if { 
> req.hdr(host) -i short.com && path_reg -i ^/\d+$ }

There are two things I see in your config that are not along the lines 
Haproxy configuration needs to be written.

1) You seem to get bitten by the way the config parser works and need to 
use quoting. Otherwise, the comma you are using to produce the 
"location" argument will not be interpreted correctly. See the 
explanations at 
https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#2.2

2) Haproxy's configuration language does not allow logical operators 
like '&&'  to form complex conditions (and, while we're at it, not 
parentheses). You'll need to cope with the use of the implicit "or" 
(within one ACL, more so for named ACLs), the implicit "and" (in between 
single ACLs) and the "or" keyword (outside of individual conditions). 
See 
https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#7.2 


(This really has to do with how you want to look at it, but from the 
formal computer science perspective, to construct a valid Haproxy 
configuration, you need to express yourself in "conjunctive normal form".)

Putting aside theoretical thoughts, as far as I understand what you're 
trying to do, you could write the above line like this:

http-request redirect location "https://example.com/id%[path,ltrim(/)]" 
if { req.hdr(host) -i short.com } { path_reg -i ^/\d+$ }

This is at least valid Haproxy config syntax, although I'm not 100 
percent sure this does what you intend to do.

Another way to tackle a substitution like that would be to use "regsub", 
as you were using a regular expression already. But then, with regsub 
and backreferences like "\1", you will really need to look into the 
section about quoting  in the manual, as it gets a bit complicated at 
that point.

Good luck,

Jens


Reply via email to