Forum Discussion

Shawn_Salyers_8's avatar
Shawn_Salyers_8
Icon for Nimbostratus rankNimbostratus
Apr 27, 2011

URI Redirect with 301

Hello and thank you for the help....

 

 

I have a need to take the incoming request and replace portions of the URI and redirect to ta different pool, which is currently working with the following iRule:

 

 

when HTTP_REQUEST {

 

if {[string tolower [HTTP::host]] eq "devurl.mycompany.com" } {

 

if { [HTTP::uri] contains "/landing/sections/innovation/article/" } {HTTP::uri [string map {/landing/sections/innovation/article /articles} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/lifestyle/article/" } {HTTP::uri [string map {/landing/sections/lifestyle/article /articles} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/managing/article/" } {HTTP::uri [string map {/landing/sections/managing/article /articles} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/marketing/article/" } {HTTP::uri [string map {/landing/sections/marketing/article /articles} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/money/article/" } {HTTP::uri [string map {/landing/sections/money/article /articles} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/technology/article/" } {HTTP::uri [string map {/landing/sections/technology/article /articles} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/the-world/article/" } {HTTP::uri [string map {/landing/sections/the-world/article /articles} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/innovation/videos/" } {HTTP::uri [string map {/landing/sections/innovation/videos /videos} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/lifestyle/videos/" } {HTTP::uri [string map {/landing/sections/lifestyle/videos /videos} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/managing/videos/" } {HTTP::uri [string map {/landing/sections/managing/videos /videos} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/marketing/videos/" } {HTTP::uri [string map {/landing/sections/marketing/videos /videos} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/money/videos/" } {HTTP::uri [string map {/landing/sections/money/videos /videos} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/technology/videos/" } {HTTP::uri [string map {/landing/sections/technology/videos /videos} [HTTP::path]][HTTP::query]}

 

elseif { [HTTP::uri] contains "/landing/sections/the-world/videos/" } {HTTP::uri [string map {/idea-ub/sections/the-world/videos /videos} [HTTP::path]][HTTP::query]}}

 

 

pool POOL-X001

 

}

 

 

 

What I am being asked to do now, is add a 301 to the redirect.

 

 

Any help would be greatly appreciated.

 

 

 

--Shawn

 

 

4 Replies

  • You can change from HTTP::uri to HTTP::respond 301 Location $new_location. If you're using string map to dynamically set the new location, you can use this:

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/http__respond

     

     

    HTTP::respond 301 Location [string map {/idea-ub/sections/the-world/videos /videos} [HTTP::path]][HTTP::query]

     

     

    Also, you could use a switch statement to more efficiently check the requested URI:

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/switch

     

     

    Aaron
  • Hoolio sweeps in for the win. üòÜ

    Here's an example of what he's talking about:

    when HTTP_REQUEST {
    if {[string tolower [HTTP::host]] eq "devurl.mycompany.com" } {
    switch -glob [string tolower [HTTP::uri]] {
    "/landing/sections/innovation/article/*" {
    HTTP::respond 301 Location [string map {/landing/sections/innovation/article /articles} [HTTP::path]][HTTP::query]
    }
    "/landing/sections/lifestyle/article/*" {
    HTTP::respond 301 Location [string map {/landing/sections/lifestyle/article /articles} [HTTP::path]][HTTP::query]
    }
    }
    pool POOL-X001
    }
    }
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    With switch not only will things become far more legible (in my personal opinion, anyway) but you'll also see a pretty significant performance improvement (fact, not opinion). It's definitely worth taking a look.

     

     

    Colin
  • Thanks for all the great help! I couldn't have gotten it working without you guys!