Forum Discussion
Pav_70755
Nimbostratus
Sep 20, 2011HTTP 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
20 Replies
- Brian_69413
Nimbostratus
A couple things:
1. Are you trying to replace the existing iRule with the new requirements or merge them together? I see a conflict with the first else if.
2. Put "STAGE_www.new.com-80" in as the default pool and get rid of the else statement as it is unnecessary.
3. You are going to have to use string manipulation to get this to work right. I listed the process below(this is not code):
-Find out if it matches the condition: [HTTP::uri] starts_with "/win"
-Modify the original uri: A new variable with a substr of [HTTP::uir] to get rid of the "/win"
-Redirect to the new URL: HTTP::redirect [HTTP::host]+"/News-Advice"+$new_var - Pav_70755
Nimbostratus
Hi Brian,
I forgot to remove the first line but i think the rule needs to be modified to have some paramaters set that replace something from an existing url or use a parameter thats set to insert into the re-directed urlwhen HTTP_REQUEST { set extracta [URI::query [HTTP::query] "New-Advice" ] if { [HTTP::uri] ends_with "/win" } { HTTP::redirect "http://www.new.com/$extracta" }} elseif { [HTTP::uri] ends_with "/talk" } { HTTP::redirect "http://www.news.comk/Marketing-Services/" } elseif { [HTTP::uri] ends_with "/find" } { HTTP::redirect "http://www.news.comk/Business-Data/" } else { pool STAGE_www.news.com-80 } }
Would something like that work for the first line? - Brian_69413
Nimbostratus
I think you would do the following:when HTTP_REQUEST { if { [HTTP::uri] starts_with "/win" } { set extracta [substr [HTTP::uri] "/win" ""] HTTP::redirect "http://www.new.com/New-Advice$extracta" } elseif { [HTTP::uri] ends_with "/talk" } { HTTP::redirect "http://www.news.comk/Marketing-Services/" } elseif { [HTTP::uri] ends_with "/find" } { HTTP::redirect "http://www.news.comk/Business-Data/" } else { pool STAGE_www.news.com-80 } } - Pav_70755
Nimbostratus
Thanks Brian I'm getting the following error with that rule:
line 3: [invalid integer] ["/win"]
I'm essentially needing the following url
www.news.com/win/News-Archive/Marketing-News/?storyId=84494
to be replaced with
www.news.com/News-Advice/News-Archive/Marketing-News/?storyId=84494
so "win" to be replaced with "New-Advice" in the uri and also some additional ones now
"talk" to be replaced with "Marketing-Services"
"find" to be replaced with "Business-Data"
Here is the rule modified slightlywhen HTTP_REQUEST { if { [HTTP::uri] starts_with "/win" } { set wininsert [substr [HTTP::uri] "/win" ""] HTTP::redirect "http://www.new.com/New-Advice$wininsert" } elseif { [HTTP::uri] starts_with "/talk" } { set talkinsert [substr [HTTP::uri] "/talk" ""] HTTP::redirect "http://www.new.com/Marketing-Services$talkinsert" } elseif { [HTTP::uri] starts_with "/find" } { set findinsert [substr [HTTP::uri] "/find" ""] HTTP::redirect "http://www.new.com/New-Advice$findinsert" } else { pool STAGE_www.news.com-80 } } - Brian_69413
Nimbostratus
Not sure why substr is not working...you could also try a string trimleft since you know exactly what the values are. Replace the substr lines with these:set wininsert string trimleft [HTTP::uri] 4 set talkinsert string trimleft [HTTP::uri] 5 set findinsert string trimleft [HTTP::uri] 5
There are many other ways such as using regsub, string map or other string manipulation functions - Pav_70755
Nimbostratus
Thanks Brian i'm getting the following error:
line 3: [wrong args] [set wininsert string trimleft [HTTP::uri] 4]
line 7: [wrong args] [set talkinsert string trimleft [HTTP::uri] 5]
line 11: [wrong args] [set findinsert string trimleft [HTTP::uri] 5]when HTTP_REQUEST { if { [HTTP::uri] starts_with "/win" } { set wininsert string trimleft [HTTP::uri] 4 HTTP::redirect "http://www.new.com/New-Advice$wininsert" } elseif { [HTTP::uri] starts_with "/talk" } { set talkinsert string trimleft [HTTP::uri] 5 HTTP::redirect "http://www.new.com/New-Advice$talkinsert" } elseif { [HTTP::uri] starts_with "/find" } { set findinsert string trimleft [HTTP::uri] 5 HTTP::redirect "http://www.new.com/New-Advice$findinsert" } else { pool STAGE_www.new.com-80 } } - Brian_69413
Nimbostratus
You probably have to enclose the actual function in square brackets:set wininsert [string trimleft [HTTP::uri] 4] - Pav_70755
Nimbostratus
Thanks Brian i'm still learning when it comes to Irules as you can see! so whats the timleft argument doing?
The Irule works now although its doing the following, when I go to
http://www.new.com/win/News-Archive/
its re-directing to
http://www.new.com/News-Advice/win/News-Archive/
instead of http://www.new.com/News-Advice/News-Archive/
its almost there - Brian_69413
Nimbostratus
Wow, I completely misinterpreted string trimleft. I thought the chars variable was an integer of how many characters you wanted to trim from the left, but instead, it is a list of characters that you want to match, so it would be something like this:set wininsert [string trimleft [HTTP::uri] /win]
This is very dangerous though since it could strip other parts of the URI away...I guess regsub string match is the best bet for this. - Brian_69413
Nimbostratus
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]
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
