Forum Discussion
Pav_70755
Sep 20, 2011Nimbostratus
HTTP Re-Direct uri replace?
I have the following rule written
when HTTP_REQUEST {
if { [HTTP::uri] contains "?storyId=" } {
pool STAGE_directmarketing.thomsonnet.co.uk-80
}
elseif { [HTTP::uri] starts_with "/win" or [HTTP::uri] starts_with "/Win" }{
HTTP::redirect "http://www.news.com/Marketing-Services/"
}
elseif { [HTTP::uri] ends_with "/talk" } {
HTTP::redirect "http://www.news.com/Marketing-Services/"
}
elseif { [HTTP::uri] ends_with "/find" } {
HTTP::redirect "http://www.news.com/Business-Data/"
} else {
pool STAGE_www.news.com-80
}
}
And what I've been asked to do is the following:
anything with "/win" in the uri to replace it with "/News-Advice/
e.g if someone has the following url bookmarked
www.news.com/win/News-Archive it should go to
www.news.com/News-Advice/News-Archive
any help would be much apprecaited.
Thanks
Pav
- Pav_70755NimbostratusHI Brian,
when HTTP_REQUEST { if { [HTTP::uri] starts_with "/win" } { set wininsert [string trimleft [HTTP::uri] /win] HTTP::redirect "http://www.news.com/News-Advice/$wininsert" } elseif { [HTTP::uri] starts_with "/Win" } { set wininsert [string trimleft [HTTP::uri] /Win] HTTP::redirect "http://www.news.com/News-Advice/$wininsert" } elseif { [HTTP::uri] starts_with "/talk" } { set talkinsert [string trimleft [HTTP::uri] /talk] HTTP::redirect "http://www.news.com/Marketing-Services/$talkinsert" } elseif { [HTTP::uri] starts_with "/Talk" } { set talkinsert [string trimleft [HTTP::uri] /Talk] HTTP::redirect "http://www.news.com/Marketing-Services/$talkinsert" } elseif { [HTTP::uri] starts_with "/find" } { set findinsert [string trimleft [HTTP::uri] /find] HTTP::redirect "http://www.news.com/Business-Data/$findinsert" } elseif { [HTTP::uri] starts_with "/Find" } { set findinsert [string trimleft [HTTP::uri] /Find] HTTP::redirect "http://www.news.com/Business-Data/$findinsert" } else { pool STAGE_www.news.comk-80 } }
- Pav_70755NimbostratusPosted By Brian on 09/21/2011 07:02 AM I tested this one and it works...maybe someone can come up with something better:
set wininsert [string range [HTTP::uri] 4 end] set talkinsert [string range [HTTP::uri] 5 end] set findinsert [string range [HTTP::uri] 5 end]
- Brian_69413NimbostratusDid you try the range commands above?
- Brian_69413NimbostratusThe range command creates a new string of the URI minus the first 4 or 5 characters.
- Pav_70755NimbostratusPosted By Brian on 09/21/2011 07:26 AM The range command creates a new string of the URI minus the first 4 or 5 characters.
HTTP::respond 301 "location"
- Brian_69413NimbostratusYes, you have the right syntax there.
- Pav_70755Nimbostratus
elseif { [HTTP::uri] starts_with "/win" } { set wininsert [string range [HTTP::uri] 4 end] HTTP::respond 301 "Location" "http://www.news.com/News-Advice$wininsert"
- hooleylistCirrostratusHere's an option which expands on Brian's examples. The iRule cannot send a 302. So if you're seeing a 302 at the client, it's most likely coming from the web application.
when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { "/win*" { HTTP::respond 301 Location "http://www.news.com/News-Advice/[string range [HTTP::uri] 4 end]" } "/talk*" { HTTP::respond 301 Location "http://www.news.com/Marketing-Services/[string range [HTTP::uri] 5 end]" } "/find*" { HTTP::respond 301 Location "http://www.news.com/Business-Data/[string range [HTTP::uri] 5 end]" } default { pool STAGE_www.news.comk-80 } } }
- Pav_70755NimbostratusThanks Aaron I've managed to get the rule working as follows
when HTTP_REQUEST { if { [HTTP::uri] starts_with "/win" or [HTTP::uri] starts_with "/Win" } { set wininsert [string range [HTTP::uri] 4 end] HTTP::respond 301 "location" "http://www.news.com/News-Advice$wininsert" } elseif { [HTTP::uri] starts_with "/talk" or [HTTP::uri] starts_with "/Talk" } { set talkinsert [string range [HTTP::uri] 5 end] HTTP::respond 301 "Location" "http://www.news.com/Marketing-Services$talkinsert" } elseif { [HTTP::uri] starts_with "/find" or [HTTP::uri] starts_with "/Find" } { set findinsert [string range [HTTP::uri] 5 end] HTTP::respond 301 "Location" "http://www.news.com/Business-Data$findinsert" } elseif { [HTTP::uri] contains "2fwusjt568" } { pool STAGE_www.news.com-80 } } when HTTP_RESPONSE { if { [HTTP::status] == 302 } { HTTP::respond 301 Location [HTTP::header Location] } }
- hooleylistCirrostratusI don't think you'd want to rewrite all 302s to 301s as that tells the client and any intermediate proxies to permanently cache the redirect.
when HTTP_RESPONSE { if { [HTTP::status] == 302 and not ([HTTP::header Location] contains "2fwusjt568")} { HTTP::respond 301 Location [HTTP::header Location] } }
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