Forum Discussion
raytoles_75680
Nimbostratus
Nov 08, 2009Redirects for old site - iRule Newbie needs help
We have to support some redirects for old sites after our one of our new sites goes live. Our plan is the change our dns such the the old site request are sent to our new F5 virtual server. But I received a new requirement which requires a redirect for existing product items.
Requirements:
http://oldsite..com/itemnumber.html redirect http://newsite.org/scrubs/imagine/itemnumber.aspx
and
http://oldsite.com redirect to http://newsite.org/scrubs/imagine
Here's what I have, and it does not work:
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::host]] {
"oldsite.com" {
if a uri does not exist
if { [HTTP::uri] equals "/" } {
log local0. "URI is empty, redirect to /scrubs/imagine"
HTTP::redirect "http://newsite.org/scrubs/imagine"
if a uri exist
} else {
log local0. "URI exists, redirect to /scrubs/imagine/uri"
HTTP::redirect "http://newsite.org/scrubs/imagine/[HTTP::uri]"
}
}
}
}
8 Replies
- The_Bhattman
Nimbostratus
Try it this waywhen HTTP_REQUEST { if {[string tolower [HTTP::host]] eq "oldsite.com" } { switch -glob [HTTP::uri] { "/" { log local0. "redirect on empty URI" HTTP::redirect "http://newsite.org/scrubs/imagine " } "/itemnumber.html" { log local0. "redirect on itemnumber" HTTP::redirect "http://newsite.org/scrubs/imagine/itemnumber.aspx" } } } }
I hope this helps
CB - raytoles_75680
Nimbostratus
Thanks CB. We have about 50+ item numbers to support. So "itemnumber".html can be 400x, 401x, and so forth. We want to redirect the to the corresponding "itemnumber".aspx if it exist in the request.
http://oldsite.com/itemnumber.html redirect to http://newsite.org/scrubs/imagine/sameitemnumber.aspx.
Requests for the root of the oldsite should go to the root of the new site.
http://oldsite.com/ redirect to http://newsite.org/scrubs/imagine
Sorry I was not clear on this. I appreciate it. - The_Bhattman
Nimbostratus
So what you are saying is you can have the following:http://oldsite.com/400x.html to http://newsite.org/scrubs/imagine/400x.aspx
CB - raytoles_75680
Nimbostratus
Yes, where the itemnumber can be 401x.html, 402x.html and so on. - The_Bhattman
Nimbostratus
How about thiswhen HTTP_REQUEST { if {[string tolower [HTTP::host]] eq "oldsite.com" } { switch -glob [HTTP::uri] { "/" { log local0. "redirect on empty URI" HTTP::redirect "http://newsite.org/scrubs/imagine " } default { set base [URI::basename [HTTP::uri]] log local0. "This is the base variable $base" set sbase [split $base .] log local0. "this is the base split by the . : $sbase" set itemn [lindex $sbase 0] log local0. "redirect on itemnumber without the extension: $itemn" HTTP::redirect "http://newsite.org/scrubs/imagine/$itemn.aspx" } } } }
This is also making an assumption that you will either have no URI or a uri with a valid . at the end of the URI.
CB - raytoles_75680
Nimbostratus
Hot dang. Worked like a charm. I TRUELY APPRECIATE YOUR HELP! - The_Bhattman
Nimbostratus
You can try combining the elements
For example
set itemn [lindex [split [URI::basename [HTTP::uri]] . ] 0 ]
This would eliminate 3 "set" statements
Example:when HTTP_REQUEST { if {[string tolower [HTTP::host]] eq "oldsite.com" } { switch -glob [HTTP::uri] { "/" { log local0. "redirect on empty URI" HTTP::redirect "http://newsite.org/scrubs/imagine " } default { set itemn [lindex [split [URI::basename [HTTP::uri]] . ] 0 ] log local0. "redirect on itemnumber without the extension: $itemn" HTTP::redirect "http://newsite.org/scrubs/imagine/$itemn.aspx" } } } }
CB - hoolio
Cirrostratus
You could also use URI::query [HTTP::uri] "parameter_name" (Click here) to retrieve the value of a query string parameter named parameter_name.
Aaron
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects