Forum Discussion
Robert_47833
Altostratus
Aug 12, 2011how to change a string ,such as add a number after some letters
Hi,dear irule
I want to change the host name to another one
for example
www.cjj.com---->www2.cjj.com
cjj.com------>cjj2.com
*.cjj.com---->*2.cjj.com
domain [HTTP::host] 2 will return cjj.com if it is "www.cjj.com"
Thanks in advance
9 Replies
- Ed_Hammond_2611
Nimbostratus
Try something like:regsub {^([^\.]*).} [HTTP::host] {${1}2.} newhost log local0. "[HTTP:host] $newhost" - Colin_Walker_12Historic F5 AccountWhile regsub would technically work, it's going to induce a lot of overhead. There are lots of other ways to go about that: scan, string map, etc.
Also, if you're looking at maintaining a list of origin -> modified domains, then you might be better of creating a data group to store them in and just do the translation in the code, rather than building out a complex regsub or the like.
Colin - hoolio
Cirrostratus
I think something like this should work:switch [string tolower [HTTP::host]] { "cjj.com" { HTTP::header replace Host "cjj2.com" } default { HTTP::header replace Host "[getfield [HTTP::host] "cjj2.com" 1]2.cjj.com" } }
Aaron - Robert_47833
Altostratus
Hi,all friends,I use irule below to achieve this:
switch -glob [string tolower [HTTP::host]] {
"srwd29.com" -
"www.srwd29.com" {
scan [HTTP::host] {%[^.]%s} domainfirst domainleft
set num 2
HTTP::redirect "http://$domainfirst$num$domainleft[HTTP::uri]"
}
I try to add "2" after $domainfirst( HTTP::redirect "http://$domainfirst2$domainleft[HTTP::uri]"),but it doesn't work ,that is why there is a num variable
Thank you for your great help - Colin_Walker_12Historic F5 AccountI'm glad to see that you got it working. Bonus points for using the scan command, very cool and efficient. ;)
Colin - Robert_47833
Altostratus
haha,thanks,I have learned a lot from here
Thanks! - Robert_47833
Altostratus
Hi,by the way,do u know why I can't add number "2" in HTTP::redirect "http://$domainfirst2$domainleft[HTTP::uri],how to achieve this?
Did I miss something? - Colin_Walker_12Historic F5 AccountIt's not working because it doesn't know where the first variable stops, and is trying to incorporate the 2 as part of the variable name.
Try this instead:
HTTP::redirect "http://${domainfirst}2${domainleft}[HTTP::uri]"
Here's an example in tclsh:% set h1 www.srwd www.srwd % set h2 9.com 9.com % puts $h1$h2 www.srwd9.com % puts $h12$h2 can't read "h12": no such variable % puts ${h1}2${h2} www.srwd29.com %
Colin - Robert_47833
Altostratus
oh,Colin
great,it works
thanks
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
