Forum Discussion
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
- Michael_Jenkins
Cirrostratus
Both absolute and relative paths in a Location header are allowed. (See the Wikipedia article). So it should work fine either way.
- oshaughnessy_19
Nimbostratus
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.
- 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.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com