Forum Discussion

jony23's avatar
jony23
Icon for Nimbostratus rankNimbostratus
Aug 30, 2018

replace path and query with "unknown" path

Hi, I need some help please, We have the next url https://www.host.com/abc/def/ghi?query=value&query1=value1 and the query could change depending on the request. We have to do a 301 redirect to a new path tottaly different without query but the path can change depending on the requests... Eg: new request https://www.host.com/bla/bla/. The path /bla/bla/ can change depending on the client request. Any advice please...

 

3 Replies

  • Hi,

     

    it's not totaly clear can you please provide us a clear example.

     

    Regards

     

  • Hi, you say in your post that the redirect has to be total different? How do you mean? Different for each request?

    The

    [HTTP::path]
    command in an iRule will return everything up to the query. So in your example: https://www.host.com/abc/def/ghi?query=value&query1=value1 [HTTP::path] would return /abc/def/ghi

    So you can do a 301 redirect to https://www.host.com/abc/def/ghi using the following command

    HTTP::redirect https://[HTTP::host][HTTP::path]

    But I'm not sure if this is exactly what you're asking, please could you be more specific?

  • After seeing the logs I can see that the keys as well as the values can be different, so this required a slightly different approach.

    This time the iRule will read the query, break it apart into a list and loop through each query, appending the value of each query key to a new URI. Once complete it will add this new, dynamically built URI to the redirect.

    I've tested it and it seems to do what's required. Please check and let me know how you get on

    when HTTP_REQUEST {
         split each query into a list using & as separator 
        set queryList [split [HTTP::query] "&"]
        set newUri "/path/"
        set i 0
    
        if { [HTTP::host] equals "www.host.com"} {
            if { [HTTP::uri] starts_with "/path/" } {
    
                 loop through each query and append value to $newUri
                foreach param $queryList {
                    set query [split [lindex $queryList $i] "="]
                    set value [URI::query [HTTP::uri] [lindex $query 0]]
                    append newUri $value /
                    incr i
                }
                 redirect to new URI after looping has finished
                log local0. "new URI is $newUri - redirecting"
                HTTP::respond 301 noserver Location "https://[HTTP::host]$newUri"
            }
        }
    }
    

    Lee