Technical Forum
Ask questions. Discover Answers.
cancel
Showing results for 
Search instead for 
Did you mean: 
Custom Alert Banner

F5 ADC - url rewrite redirect with parsing

OMA67
Nimbostratus
Nimbostratus

F5 ADC - url rewrite redirect with parsing

Hello,

I would like to rewrite/redirect a url with parsing a part of old url into the new one

Vocabulary : The Siret number is used to identify geographical location your company and each establishment that makes it up.

Exemple 1 :

From
https://mywebsite.com/oldpath/some-text-with-hypen-siretnumber-uniqueid
To
https://mywebsite.com/newpath/subpath/uniqueid

Exemple 2 :

From
https://mywebsite.com/oldpath/some-text-with-more-hypen-siretnumber-uniqueid
To
https://mywebsite.com/newpath/subpath/uniqueid


So far I tried regex to parse the digits after the last hyphen with no success, I surely miss something, or doing something wrong, here what I put in F5 ADC :

 

when HTTP_REQUEST {
        if {[string tolower [HTTP::uri]] starts_with "/oldpath/"}{
        scan [URI::basename [HTTP::uri]] {%[\-([^-][0-9]*)$]%d} num
                HTTP::redirect "https://[HTTP::host]/newpath/subpath/${num}"
                }
}

 

Thank you for any help.

Have a nice day.

1 ACCEPTED SOLUTION

lnxgeek
MVP
MVP

Not tested:

when HTTP_REQUEST {
    # Capture the URI from the HTTP request
    set uri [HTTP::uri]
    
    # Define the pattern to extract "uniqueid" using Regular Expression
    # The pattern now captures only numerical characters after the last hypen ("-")
    set pattern {.*-(\d+)$}
    
    # Apply the regular expression to capture "uniqueid"
    if {[regexp $pattern $uri ignore uniqueid]} {
        
        # Create the new URI by appending the captured "uniqueid"
        set new_uri "/newpath/subpath/$uniqueid"
        
        # Perform HTTP redirect to the new URI
        HTTP::redirect "https://mywebsite.com$new_uri"
    }
}

View solution in original post

9 REPLIES 9

lnxgeek
MVP
MVP

Can you give some examples of what the strings/numbers look like?

Here an example : /First_name-Family_name-jobètype-41383279100011-1

And I have to keep the last number in this example 1

Paulius
MVP
MVP

@OMA67 Will the uniqueid always be at the end of the path, is it always the same length, and is it always certain characters?

Yes uniqueid wich is only digits and it is an incrementation number from 1 to infinite, it is not always the same lenght

uniqueid always at the end

lnxgeek
MVP
MVP

Not tested:

when HTTP_REQUEST {
    # Capture the URI from the HTTP request
    set uri [HTTP::uri]
    
    # Define the pattern to extract "uniqueid" using Regular Expression
    # The pattern now captures only numerical characters after the last hypen ("-")
    set pattern {.*-(\d+)$}
    
    # Apply the regular expression to capture "uniqueid"
    if {[regexp $pattern $uri ignore uniqueid]} {
        
        # Create the new URI by appending the captured "uniqueid"
        set new_uri "/newpath/subpath/$uniqueid"
        
        # Perform HTTP redirect to the new URI
        HTTP::redirect "https://mywebsite.com$new_uri"
    }
}

Thanks, I will try

 

Thank you very much ! it works like a charm ! 😀👍

Good to hear - just love to help out 🙂