Forum Discussion
Kalibre46_28860
Nimbostratus
10 years agoiRule to redirect a URL but replace only a portion of the variable length URI??
Hi,
I'll start by saying I'm new to iRules but have been a Networking professional for many years, so please go gently on the code newbie!
There is a redirect irule that I need to create th...
cjunior
Nacreous
10 years agoHi, I don't know if I clearly understood, so I would try another way:
1) Single string pattern for documents path (no regsub)
when HTTP_REQUEST {
if { [string tolower [getfield [HTTP::host] ":" 1]] equals "oldsharepoint.uk" } {
if { [string match -nocase "/*/*/documents/*" [HTTP::path]] } {
set uri [string range [HTTP::uri] [string first /documents [string tolower [HTTP::uri]]] end]
HTTP::respond 301 noserver Location "http://newsharepoint.uk/new${uri}"
unset uri
}
}
}
2) data group rewrite (I'd recommend for large list):
ltm data-group internal dg_redirect_shpt_paths {
records {
/a/1234/ {
data /new/
}
/b/1/ {
data /new2/
}
}
type string
}
when HTTP_REQUEST {
if { [string tolower [getfield [HTTP::host] ":" 1]] equals "oldsharepoint.uk" } {
set p [URI::path [string tolower [HTTP::uri]] 1 2]
set m [class match -value $p equals dg_redirect_shpt_paths]
if { $m ne "" } {
HTTP::respond 301 noserver Location "http://newsharepoint.uk[string map -nocase "$p $m" [HTTP::uri]]"
}
unset -nocomplain p m
}
}
3) rewrite with array list:
when CLIENT_ACCEPTED {
I don't like using statics variables, so here it is and not in RULE_INIT
array set redirect_shpt_paths {
/a/1234/ /new/
/b/1/ /new2/
}
}
when HTTP_REQUEST {
if { [string tolower [getfield [HTTP::host] ":" 1]] equals "oldsharepoint.uk" } {
set p [URI::path [string tolower [HTTP::uri]] 1 2]
foreach m [array names redirect_shpt_paths] {
if { $m eq $p } {
set uri [string map -nocase "$p $redirect_shpt_paths($m)" [HTTP::uri]]
HTTP::respond 301 noserver Location "http://newsharepoint.uk$uri"
}
}
unset -nocomplain p m uri
}
}
when CLIENT_CLOSED {
unset -nocomplain redirect_shpt_paths
}
If I'm wrong, I apologize,
Respectfully.
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