Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

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]]
0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Both absolute and relative paths in a Location header are allowed. (See the Wikipedia article). So it should work fine either way.

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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.

0
Comments on this Answer
Comment made 09-Sep-2015 by Rory Hewitt F5 107
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.
0