Forum Discussion

Robert_47833's avatar
Icon for Altostratus rankAltostratus
Aug 12, 2011

how 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>>





domain [HTTP::host] 2 will return if it is ""



Thanks in advance



9 Replies

  • Try something like:

    regsub {^([^\.]*).} [HTTP::host] {${1}2.} newhost
    log local0. "[HTTP:host]  $newhost"

  • Colin_Walker_12's avatar
    Historic F5 Account
    While 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.





  • I think something like this should work:

    switch [string tolower [HTTP::host]] {
       "" {
          HTTP::header replace Host ""
       default {
          HTTP::header replace Host "[getfield [HTTP::host] "" 1]"

  • Hi,all friends,I use irule below to achieve this:


    switch -glob [string tolower [HTTP::host]] {


    "" -


    "" {


    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_12's avatar
    Historic F5 Account
    I'm glad to see that you got it working. Bonus points for using the scan command, very cool and efficient. ;)



  • 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_12's avatar
    Historic F5 Account
    It'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      
    % set h2
    % puts $h1$h2
    % puts $h12$h2
    can't read "h12": no such variable
    % puts ${h1}2${h2}