For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

Rory_Hewitt_F5_'s avatar
Sep 09, 2015

Performing a redirect using string map

I'm trying to ensure that users who request a page with an older format URL get redirected to the new format URL. Would the following work correctly?

 

if {[HTTP::path] starts_with "/shop/wedding-registry/product/"} {
    HTTP::respond 301 Location [string map {"/shop/wedding-registry/product/" "/shop/registry/wedding/product/"} [HTTP::uri]]
}

 

My concern is about whether the Location must contain the full URL, including protocol and domain. If so, would this work:

 

HTTP::respond 301 Location http://[HTTP::host][string map {"/shop/wedding-registry/product/" "/shop/registry/wedding/product/"} [HTTP::uri]]

 

3 Replies

  • I believe you can use HTTP::redirect and just specify the new host without the protocol. I think you could process the string a little more quickly with an explicit string replace instead of string map, too, and save the system from having to search the string for a pattern because you know it's always going to be the first 31 characters of the string.

    So instead, you could do something like this:

     

    if { [HTTP::path] starts_with {/shop/wedding-registry/product/} } {
        set uri [string replace [HTTP::uri] 0 30 {/shop/registry/wedding/product/}]
        HTTP::redirect "//$uri"
    }
    

     

    I imagine it would be even faster to use a Rewrite profile, but if you're already processing iRules, it might be more discoverable to keep the logic all together here.

    Edit: As noted in the docs for HTTP::redirect, it does give you a 302 response code, so if you want 301, you'd have to use HTTP::respond instead.

    Just to make the reference convenient, here's the HTTP module documentation page.

     

    • Rory_Hewitt_F5_'s avatar
      Rory_Hewitt_F5_
      Icon for Cirrus rankCirrus
      Michael J, I didn't know you could do a relative URL redirect. You learn something new every day! @oshaughnessy, yeah, I'd already seen the limitation in HTTP::redirect about it only doing a 302 - that's why I used HTTP::respond. But it's useful info for the next person, so thanks for that.