cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP Redirection iRules

Karlsen_58024
Nimbostratus
Nimbostratus

Hi,

 

A customer wanted a new web solution and chose Big-IP 3900 to use as a load balancer. They want to migrate from the existing environment to the new one with as little downtime / 404 errors as possible by redirecting old links to the corresponding site in the new environment.

 

This has led me to create a ton of redirection rules. I am pretty new to Big-IP, so I would like to know what the best practices are around handelig a lot of URIs. This is how I solved it:

 

 

 

when HTTP_REQUEST {

 

if { ([string tolower [HTTP::host]] contains "example.com") and [HTTP::uri] contains "/thisisanoldsite" } {

 

HTTP::redirect "https://www.example.com/this/is/the/equivilant/site"

 

}

 

if { ([string tolower [HTTP::host]] contains "example.com") and [HTTP::uri] contains "/hey/whatsup.aspx" } {

 

HTTP::redirect "https://www.example.com/abc/def/example.aspx"

 

}

 

if { ([string tolower [HTTP::host]] contains "example.com") and [HTTP::uri] contains "/page206.aspx" } {

 

HTTP::redirect "https://www.example.com/subsite/URI/page.aspx"

 

}

 

if { ([string tolower [HTTP::host]] contains "example.com") and [HTTP::uri] contains "/AboutThisPage" } {

 

HTTP::redirect "https://www.example.com/About/Page.aspx"

 

}

 

...

 

}

 

}

 

This rule is applied on the HTTP Virtual Server. As you can see, everything is redirected to https://, so this iRule will only be applicable on the users first request.

 

 

This is just a small part of it. The complete rule contains about 1200 URIs, devided in two seperate iRules.

 

 

 

Can this be changed into something a little less complex? Using switch insted of if's? I would also like if these rules replied with a HTTP 301 Permanently moved status code. Should I replace every HTTP::redirect with HTTP::respond 301 location ?

 

 

In advance, thank you.

 

 

Regards,

 

Martin

 

4 REPLIES 4

nitass
F5 Employee
F5 Employee
what about this one?

 

 

[root@ve11a:Active:Changes Pending] config tmsh list ltm virtual bar ltm virtual bar { destination 172.28.19.252:443 ip-protocol tcp mask 255.255.255.255 profiles { clientssl { context clientside } http { } tcp { } } rules { myrule } snat automap vlans-disabled } [root@ve11a:Active:Changes Pending] config tmsh list ltm data-group internal redirection_class ltm data-group internal redirection_class { records { /AboutThisPage { data /About/Page.aspx } /hey/whatsup.aspx { data /abc/def/example.aspx } /page206.aspx { data /subsite/URI/page.aspx } /thisisanoldsite { data /this/is/the/equivilant/site } } type string } [root@ve11a:Active:Changes Pending] config tmsh list ltm rule myrule ltm rule myrule { when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "example.com" } { if { [class match -- [HTTP::uri] equals redirection_class] } { HTTP::respond 301 noserver Location "https://www.example.com[class match -value [HTTP::uri] equals redirection_class]" Connection Close } } } } [root@ve11a:Active:Changes Pending] config curl -Ik https://example.com/AboutThisPage HTTP/1.0 301 Moved Permanently Location: https://www.example.com/About/Page.aspx Connection: close Content-Length: 0

Karlsen_58024
Nimbostratus
Nimbostratus

Thank you, nitass!

 

 

That was exactly what I needed.

 

 

- Karlsen

 

Karlsen_58024
Nimbostratus
Nimbostratus
Thank you, nitass!

 

 

That was exactly what I needed.

 

 

- Karlsen

 

Brian_Deitch_11
Historic F5 Account
If you are not a fan of multiple IF statements or data groups you could do nested switches:

 

when HTTP_REQUEST { switch -glob [ string tolower [HTTP::host]] { *example.com* { switch -glob [ string tolower [HTTP::uri]] { *thisisanoldsite* { HTTP::redirect "https://www.example.com/this/is/the/equivilant/site" } */hey/whatsup.aspx* { HTTP::redirect "https://www.example.com/abc/def/example.aspx" } */page206.aspx* { HTTP::redirect "https://www.example.com/subsite/URI/page.aspx" } */aboutthispage* { HTTP::redirect "https://www.example.com/About/Page.aspx" } default { HTTP::respond 301 noserver Location "https://www.example.com" } } } *nextdomain.com* { switch -glob [ string tolower [HTTP::uri]] { *thisisanoldsite* { HTTP::redirect "https://www.nextdomain.com/this/is/the/equivilant/site" } */hey/whatsup.aspx* { HTTP::redirect "https://www.nextdomain.com/abc/def/nextdomain.aspx" } */page206.aspx* { HTTP::redirect "https://www.nextdomain.com/subsite/URI/page.aspx" } */aboutthispage* { HTTP::redirect "https://www.nextdomain.com/About/Page.aspx" } default { HTTP::respond 301 noserver Location "https://www.nextdomain.com" } } } } }