Forum Discussion

southern_shredd's avatar
Oct 17, 2022
Solved

redirect to to 301 location

We are trying to come up with an irule that will:

redirect based on id value X (eg. 144 string) to a new URL and append the same value in this new URL . So it must extract the id=X value and append it to the redirect URL 

 

Example:

https://value-same.domain.com/external/services/datastorage/#/?id=144"

 Must be redirected (301 location)

https://different-website.domain/external/services/datastorage/#/?id=144"

the value 144 could be any value. I was thinking of this 

I have tried a range of irules usind data groups but it has to get the value from the request

Does anybody have a suggestions? My irule below did not match anything but thats because of not hostname etc.

when HTTP_REQUEST {
if { [HTTP::uri] contains "/external/services/datastorage/#/?id=144"" }{
HTTP::respond 301 Location "https://different-website.domain/external/services/datastorage/#/?id=144""

}
}

 

  • CA_Valli's avatar
    CA_Valli
    Oct 18, 2022

    My bad, there's a typo.

    Edited in previous comment 

6 Replies

  • Hello, I've notciced your URL is build with a reserved character "#" , you might want to review this because hash character is used as a delimiter to separate the URI from a fragment identifier, so everything after /#/ in your query will not be sent to the server unless JavaScript code running inside the browser. If client doesn't send it of course it will also not be matched by BIGIP HTTP profile so recalling it with [HTTP::uri] for an exact match comparison will fail. 

    Other than this, matching content is pretty simple, if you have an HTTP profile attached to your VS you can use [HTTP::query] or [HTTP::uri] to recall information from client request. 

    Depending on how many matches you have on ID, you can use if/switch/datagroups to build your syntax ; this is a sample with a switch statement

     

     

    when HTTP_REQUEST {
      if { [HTTP::uri] starts_with "/external/services/datastorage/" }{
        set id [findstr [HTTP::query] "id=" 3 &]
        switch -glob $id {
           143 - 
           144 { HTTP::respond 301 Location "https://different-website.domain[HTTP::uri]" }
           145 { log local0. "this is a different match" }
           default {}
        }
        # in above switch statement , ?id=143 or ?=id 144 will match same condition ; ?id=145 will match its own condition ; every other ?id=xxx value will match default 
      }
    }

     

     

     

  • Thanks for the response, could we make it such that anything after /datastorage/XXXX  be extracted and appended to the new URL? So it does not have to be numeric values only (144,145 etc.

    Example
    /#/?id=144

    https://different-website.domain/external/services/datastorage/#/?id=144
    https://different-website.domain/external/services/datastorage/#/50b2580d-9f9f-41b7-b0af-618d570de14f/receipt
    https://different-website.domain/external/services/datastorage/#/?vi=BYR&saksnr=12221&id=144

    redirected to:

    https://different-website.domain/external/services/datastorage/#/?id=144
    https://different-website.domain/external/services/datastorage/#/50b2580d-9f9f-41b7-b0af-618d570de14f/receipt
    https://different-website.domain/external/services/datastorage/#/?vi=BYR&saksnr=12221&id=144

    So perhaps using "HTTP::query" & /path maybe

    when HTTP_REQUEST {
    if { [HTTP::uri] starts_with "/external/services/datastorage


    • CA_Valli's avatar
      CA_Valli
      Icon for MVP rankMVP

      It didn't have to be numeric, script captured everything that followed "id=" so "id=abCD456" could be a match as well. I used numeric in my test because I assumed it was. 

      Code for scripting a wildcard match is much simpler, but once again I'm concerned that all of your URLs have a fragment identifier, which (being a client-only feature) I believe is not being sent by browser -- therefore parser will not see that part with [HTTP::uri] or [HTTP::query]. 

       

       

      when HTTP_REQUEST {
        if { [HTTP::uri] starts_with "/external/services/datastorage/" }{
          HTTP::respond 301 Location "https://different-website.domain[HTTP::uri]"
          }
      }

       

       

       

      • southern_shredd's avatar
        southern_shredd
        Icon for Cirrus rankCirrus

        I got an error when trying to compile the irule

        error: [command is not valid in the current scope][}]