Forum Discussion
Troy_Atwood_942
Mar 08, 2011Nimbostratus
3 scenario 301 redirection problem
We are moving sections of our website to a new url structure and need to redirect only the sections that are moving.
Sounds like a job for IRules!
We have 3 scenarios that we need...
Troy_Atwood_942
Mar 25, 2011Nimbostratus
Here is my first iRule made possible via all of the great examples I found in the forum.
when RULE_INIT {
Debug off (0), Errors-only(1), On(2) or Verbose(3)
set ::debug 0
}
when HTTP_REQUEST {
set H [string tolower [HTTP::host]]
set uri [string tolower [HTTP::uri]]
set A [HTTP::host][HTTP::uri]
only test urls that need to be redirected.
if { [class match $uri contains jm_urls] } {
print the incoming uri so we can compare before/after results in the ltm log:
if { $::debug>=2 } { log local0. " iRule TEST input before we mess with it: $H$uri "}
Case 1: if I can match a target uri, substitue the first part, plus everything after the
old 22 character length target uri that I"m replacing.
if { $uri contains "/bn/community/jm/blog/" } {
set puri [substr $uri 22 ";"]
HTTP::respond 301 Location "http://$H/bn/blogs/dl/$puri"
if { $::debug>=2 } { log local0. " Test Case 1 satisfied "
set A "http://$H/bn/blogs/dl/$puri"
}
Case 2: now lets chase down document numbers, and redirect if we find a target:
} elseif { $uri contains "/bn/docs/doc-" } {
set puri [substr $uri 13 ";"]
HTTP::respond 301 Location "http://$H/jmredirect/doc/$puri"
if { $::debug>=2 } { log local0. " Test Case 2.1 satisfied "
set A "http://$H/jmredirect/doc/$puri"
}
} elseif { $uri contains "/bn/docs/" } {
set puri [substr $uri 9 ";"]
HTTP::respond 301 Location "http://$H/jmredirect/doc/$puri"
if { $::debug>=2 } { log local0. " Test Case 2.2 satisfied "
set A "http://$H/jmredirect/doc/$puri"
}
Case 3: lets redirect the door page. redirects. Third verse, same as the first...
} elseif { $uri ends_with "/bn/community/jm/" } {
HTTP::respond 301 Location "http://$H/dl/"
if { $::debug>=2 } { log local0. " Test Case 3 satisfied "
set A "http://$H/dl/"
}
Case 4: lets redirect everything else to the /dl/downloadinvalid.aspx page.
} else {
HTTP::respond 301 Location "http://$H/dl/downloadinvalid.aspx"
if { $::debug>=2 } { log local0. " Test Case 4 satisfied "
set A "http://$H/dl/downloadinvalid.aspx"
}
}
show me what the rewritten uri looks like in the logs. log file should have three lines of
code-generated log messages each time this iRule fires:
" iRule TEST input before we mess with it: "
" Test Case satisfied "
" iRule TEST output: "
if { $::debug>=2 } {log local0. " iRule TEST output: $A " }
}
}
Any feedback would be welcome.
Thanks,
Troy
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