Forum Discussion

Cindy_127211's avatar
Cindy_127211
Icon for Nimbostratus rankNimbostratus
Aug 16, 2006

Rewriting Hostname & URI on HTTP_Request without a 'redirect'

I would like to 'rewrite' the following hostname/uri:

 

 

http://www.website1.com/index.php?cid=1234

 

 

to

 

 

http://www.website2.com/app1/index.php?cid=1234

 

 

without using a 'redirect'. It is important on this 'rewrite' that the variable information (cid=1234) get carried over. However, I've only been able to do this with a 'redirect'. I'd prefer to just 'rewrite' the hostname/url;however, as the application code resides on the same server.

 

 

How can I make a 'rewrite' (versus a redirect) work in this scenario? Also, I 'threw' in the HTTP::header replace HOST statement...but, don't know if it is really needed.

 

 

when HTTP_REQUEST {

 

if {[HTTP::uri] starts_with "/index.php"} {

 

set newuri "/livestrongportfolios[HTTP::uri]"

 

set newhost "www.americancentury.com"

 

HTTP::header replace HOST $newhost

 

HTTP::redirect "http://$newhost$newuri"

 

}

 

else {

 

HTTP::redirect "https://[HTTP::host][HTTP::uri]"

 

}

 

}

 

7 Replies

  • You can set the uri and host directly on a request, and forward to a configured pool internal to the system without issuing a redirect:

    
    when HTTP_REQUEST {
      if {[HTTP::uri] starts_with "/index.php"} {
        HTTP::uri "/app1[HTTP::uri]"
        HTTP::host "www.website2.com"
        pool your_pool_name
      } else {
          HTTP::redirect "https://[HTTP::host][HTTP::uri]"
      }
    }

    You might want to include additional checks in your if statement to make sure the site is www.website1.com, depending on your requirements.
  • I have tried the fomrat that you suggested below, but it doesn't work. The rules editor shows a 'red' circle next to the HTTP::host statement and I am getting the following error in the 'ltm' log.

     

     

    BTW, I've also tried to put [] brackets around the HTTP::host and HTTP::uri variables, as well as use the 'set' statement in front. None of these produces the changed host or uri names according to what is logged in the ltm log.

     

     

    Aug 16 08:25:20 kc0501-bigip-03 mcpd[702]: 01070151:3: Rule [lvsredirect_w_cid_test] error: line 12: [wrong args] [HTTP::host "www.americancentury.com"]

     

     

    Following is the 'exact' rule that I've been trying to get to work:

     

     

    when HTTP_REQUEST {

     

    if {[HTTP::uri] starts_with "/index.php"} {

     

    Put uri into a variable

     

    set uriValue [HTTP::uri]

     

    Set constant string 1, 1st part

     

    set strA "/livestrongportfolios/index.php?cid="

     

    Get the cid uid

     

    set ciduid [findstr $uriValue "cid=" "4" "&"]

     

    log local0. "This is the ciduid variable: $ciduid"

     

    Set the new url

     

    set newuri [concat $strA$ciduid]

     

    HTTP::host "www.americancentury.com"

     

    HTTP::uri $newuri

     

    log local0. "This is the newuir: $newuri"

     

    log local0. "This is the new host: [HTTP::host]"

     

    log local0. "This is the new uri: [HTTP::uri]"

     

    }

     

    else {

     

    HTTP::redirect "https://[HTTP::host][HTTP::uri]"

     

    }

     

    }

     

     

  • Sorry about that..I thought setting the host with that command was valid. Try this (I'm assuming you have a pool assigned in the vip?):

    
    when HTTP_REQUEST {
      if {[HTTP::uri] starts_with "/index.php"} {
        log "Requested URL is [HTTP::host][HTTP::uri]"
        HTTP::uri "/livestrongportfolios[HTTP::uri]"
        HTTP::header replace "Host" "www.americancentury.com"
        log "New URL is [HTTP::host][HTTP::uri]"
      } else {
          HTTP::redirect "https://[HTTP::host][HTTP::uri]"
      }
    }

    I'm still a little foggy on when these values are actually set, and I think it varies depending on the command, so if you are getting the same value with the second log statement in the HTTP_REQUEST event, try putting the second log statement in the HTTP_REQUEST_SEND event instead:

    
    when HTTP_REQUEST_SEND {
      log local0. "New URL is [HTTP::host][HTTP::uri]"
    }
  • Thanks for your help with this. I am now able to resolve the 'host' name to the new name with the following statement.

     

     

     

    HTTP::header replace "Host" "www.americancentury.com"

     

    HTTP::uri $newuri

     

     

    I also tried the following statement:

     

     

    when HTTP_REQUEST_SEND {

     

    log local0. "New URL is [HTTP::host][HTTP::uri]"

     

    }

     

     

    ...but, I am getting an error in the 'ltm' log on this:

     

     

    Aug 16 08:56:25 tmm tmm[672]: 01220001:3: TCL error: Rule lvsredirect_w_cid_test - Illegal argument. Invalid server side API (line 1) invoked from within "HTTP::uri"

     

  • Sorry....I should add that the HTTP::uri is not getting reset to the new URI.
  • There were some caching optimizations put in with regards to internal variables such that if you set the value of one with the command (ie. "HTTP::uri /foo"), trying to use that value will still display the original value before the assignment.

     

     

    This doesn't mean it's not set correctly - it is. It's just that if you try to access the value before the request goes out, it will not display the current value.

     

     

    This issue seems to be showing up more regularly lately. We'll have to look into fixing this.

     

     

    -Joe
  • do a tcpdump on the serverside vlan and look for the URI. At the command line, issue the command below and then run your test. Ctrl-C will kill the process when your test is done.

     

     

    tcpdump -ni -w/var/tmp/URI_capture.cap -s0

     

     

    To read your dump, you can ssh it to your desktop to use a sniffer package, or you can read it at the command line:

     

     

    tcpdump -r/var/tmp/URI_capture.cap -Xs 1500