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.com/newFolder/page1.html the address bar of their browser doesn't change, but they actually see the contents of http://domain.com/oldFolder/page1.html.
I need the rule to be able to do this for any page or image on the site, like the following examples:
http://domain.com/newFolder/page2.html <- http://domain.com/oldFolder/page2.html
http://domain.com/newFolder/image1.gif <- http://domain.com/oldFolder/image1.gif
http://domain.com/newFolder/about/index.html <- http://domain.com/oldFolder/about/index.html
http://domain.com/newFolder/images/image2.gif <- http://domain.com/oldFolder/images/image2.gif
How do you go about doing this in iRules? Any documentation on this specific type of iRule?
Thanks in advance,
Brandon Petersen
- hooleylistCirrostratusHi Brandon,
- Michael_YatesNimbostratusHere is another option that is a little lighter:
when HTTP_REQUEST { switch -glob [ string tolower [HTTP::uri] ] { "/oldFolder/*" { HTTP::uri [ string map {"oldFolder" "newFolder"} [HTTP::uri] ] } } }
- hooleylistCirrostratusIf the app responds with /oldFolder/ in the path, you'd also want to rewrite it to /newFolder/ in redirects and response content. You can do this using using HTTP::is_redirect and a stream profile and STREAM::expression.
- Brandon_11574NimbostratusThanks Michael,
- Brandon_11574NimbostratusNow I'm onto replacing all instances of /oldFolder/ with /newFolder/ in the page content, does this look right?
- hooleylistCirrostratusHi Brandon,
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 } }
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 } } }
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