Forum Discussion

marcus_lindner_'s avatar
marcus_lindner_
Icon for Nimbostratus rankNimbostratus
Jan 11, 2006

uri rewrite based on the servername

I am trying to come up with a rule where the server name of the uri is used to rewrite the URL. an example will be:

http://server_1.de/index.html <=> http://server_4.de/server1/index.html

http://server_2.de/index.html <=> http://server_4.de/server2/index.html

http://server_3.de/index.html <=> http://server_4.de/server3/index.html

my first idea... -but is not working


when HTTP_REQUEST { 
    pool zone_xml_gw
}
when LB_SELECTED {
    if { [LB::server addr] equals "server_4.de" } {
         set newuri [HTTP::uri "/[HTTP::host][HTTP::uri]"] 
         HTTP::redirect "http://[HTTP::host]/newuri"
    }
    else {
        node server_1.de 80
node server_2.de 80
node server_3.de 80
    }
}
  • For redirects based on contents in the URI, you can do it all from within the HTTP_REQUEST event.

    Here's one way to do it.

    when HTTP_REQUEST {
      set 
      if { [HTTP::host] ends_with "server_1.de" } {
        HTTP::redirect "http://server_4.de/server1[HTTP::uri]"
      } elseif { [HTTP::host] ends_with "server_2.de" } {
        HTTP::redirect "http://server_4.de/server2[HTTP::uri]"
      } elseif { [HTTP::host] ends_with "server_3.de" } {
        HTTP::redirect "http://server_4.de/server3[HTTP::uri]"
      }
      pool zone_xml_gw
    }

    This will look for the host portion of the URI and then issue the redirects. I've used the ends_with operator as opposed to the eq operator so that all requests to that domain are supported (www.server_1.de, etc). If you don't care about this then switch back to the eq operator.

    A slightly more optimized version (using a switch instead of if/elseif) would be something like this:

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*server_1.de" {
          HTTP::redirect "http://server_4.de/server1[HTTP::uri]"
        }
        "*server_2.de" {
          HTTP::redirect "http://server_4.de/server2[HTTP::uri]"
        }
        "*server_3.de" {
          HTTP::redirect "http://server_4.de/server3[HTTP::uri]"
        }
      }
      pool zone_xml_gw
    }

    If you are needing to support many more hosts then it might make sense to use a data group in conjunction with the findclass command. A search of the forum on findclass should show you multiple examples.

    Hope this helps...

    -Joe
  • Joe, great information. How would I rewrite the URL before it gets to the client from the server. For example, I have an application that works fine internally. I'm using a proxy server for access from the internet. At one point in the process the application returns a URL to the client using the internal server name (which isn't routable via the internet). Is it possible to rewrite the url before it gets back to the client? I'm able to do basic rewrites via apache as long as the server is routable. Thanks.
  • The URI isn't returned to the client in the HTTP response.

     

     

    Here's how it works

     

     

    1. client puts URL in browser (http://www.foo.com/path)

     

    2. Browser extracts the components of the url and places them in the HTTP request

     

    GET /path HTTP/1.1

     

    Host: www.foo.com

     

    User-Agent: ...

     

    Accept: ...

     

    ...

     

     

    And the response from the server looks like this:

     

     

    HTTP/1.1 200 OK

     

    Date: ...

     

    Server: ...

     

    Last-Modified: ...

     

    Content-Length: xxxx

     

     

    payload_goes_here

     

     

    You'll see that the URL, host, path, etc are not included in the response.

     

     

    If your server is embedding full links in the payload (which is what it looks like you are reporting), then you'll have to write some iRules in the HTTP_RESPONSE event to dig through the payload and basically do a search and replace.

     

     

    I've highlighted a few examples of this on my blog (Click here)

     

     

    Search for the "Modify uri" items.

     

     

    Good luck!

     

     

    -Joe