Forum Discussion
Brandon_11574
Mar 24, 2011Nimbostratus
Rewrite Rule
I am needing to do a URL Rewrite where a visitor requests http://domain.com/newFolder/ but they actually see the content from http://domain.com/oldFolder/
So when they request http://domain....
hooleylist
Mar 25, 2011Cirrostratus
Hi Brandon,
If it's just the URI in requests and response redirects, you can use something like this:
when HTTP_REQUEST {
Check the requested URI set to lowercase
switch -glob [string tolower [HTTP::uri]] {
"/newfolder/*" {
set find "newfolder"
set replace "oldfolder"
HTTP::uri [ string map -nocase "$find $replace" [HTTP::uri]]
}
}
}
when HTTP_RESPONSE {
Check if response is a redirect
if {[HTTP::is_redirect] and [HTTP::header Location] contains $find}{
Rewrite the redirect Location header value
HTTP::header replace Location [string map -nocase "$find $replace" [HTTP::header Location]]
}
Check if response payload type is text
if {[HTTP::header value Content-Type] contains "text"}{
Set the replacement strings
STREAM::expression "@$find@$replace@"
Enable the stream filter for this response only
STREAM::enable
}
}
If you need to rewrite response content as well, you can add a blank stream profile, a custom HTTP profile with response chunking set to rechunk and this iRule:
when HTTP_REQUEST {
Track whether to rewrite responses
set rewrite 0
Check the requested URI set to lowercase
switch -glob [string tolower [HTTP::uri]] {
"/newfolder/*" {
set find "newfolder"
set replace "oldfolder"
HTTP::uri [ string map -nocase "$find $replace" [HTTP::uri]]
set rewrite 1
}
}
}
when HTTP_RESPONSE {
if {$rewrite}{
Check if response is a redirect
if {[HTTP::is_redirect] and [HTTP::header Location] contains $find}{
Rewrite the redirect Location header value
HTTP::header replace Location [string map -nocase "$find $replace" [HTTP::header Location]]
}
Check if response payload type is text
if {[HTTP::header value Content-Type] contains "text"}{
Set the replacement strings
STREAM::expression "@$find@$replace@"
Enable the stream filter for this response only
STREAM::enable
}
}
}
Aaron
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