Forum Discussion

jake_macabuag_4's avatar
jake_macabuag_4
Icon for Nimbostratus rankNimbostratus
Nov 07, 2012

URL rewrite

Hello everyone

 

I'm a newbie when it comes to irules and i am trying to create URL rewrite rule for web server load balancing. Basically, the local web server can be accessed via http://HOST/trendsnet_com_ph. I cannot access the site without the URI. If I create a VS, i need to append the URI instead of just using the URL. I created 2 irules and both are not working or I am doing it incorrectly.

 

Rule 1

 

 

when HTTP_REQUEST {

 

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

 

"/" {

 

HTTP::uri "/trendsnet_com_ph"

 

}

 

}

 

}

 

Rule 2

 

 

when HTTP_REQUEST {

 

if { [HTTP::uri] equals "/" } {

 

HTTP::uri "/trendsnet_com_ph"

 

}

 

}

 

 

 

Appreciating everyones help

 

thanks

 

7 Replies

  • Rule 2 looks just fine and is preferable over Rule 1. For starters, perhaps add some logging? log local. "Matched a URI of /" straight after the if statement and log local. "URI set to [HTTP:uri]" after you've modified it.

     

     

    This might also be related to the Virtual Server setup. Does it have a HTTP Profile assigned?
  • As "What Lied Beneath" said, Rule 2 looks better.

     

    If using switch, try to avoid the -glob switch unless using wildcards to match the outcome.

     

     

    Regarding rule 2 you could always try some good old fashioned debugging.

     

    Try the rule below and check the ltm log (/var/log/ltm) for the outcome.

     

     

    when HTTP_REQUEST {

     

     

    set uri [string tolower [HTTP::uri]]

     

     

    log local0. "Uri: $uri"

     

     

    if { $uri equals "/" } {

     

    log local0. "Replacing uri with /trendsnet_com_ph"

     

    HTTP::uri "/trendsnet_com_ph"

     

    }

     

    }

     

     

    Also, check the logs of the web server to verify that the incoming requests has the correct uri.

     

     

    The virtual server should have a default pool if one is not chosen via iRules but I reckon you've already thought about that.

     

     

    Kind regards,

     

    Patrik
  • for whatever reason, the link is changed into https:url/uri.

     

    I did tcpdump on both client side and server side (SNAT). Initial request shows http traffic for both side, but after some time, the F5 reply to the client side becomes https. if I manually change it to http in the browser, it will connect. any idea?
  • this happens when I use Google Chrome. But if I use Internet Explorer, it does not rewrite the URL
  • uri is probably a reserved word for a variable, and it's probably not a good to use it in your iRule. Your use of a variable seems quite wasteful anyway, so perhaps try this;

    
    when HTTP_REQUEST {
     log local0. "Initial [HTTP::uri]"
      if { [HTTP::uri] equals "/" } {
       log local0. "Replacing URI with /trendsnet_com_ph"
       HTTP::uri "/trendsnet_com_ph"
       }
    }
    
  • Assuming it's not the use of $uri that's caused your further issue (and I'm hoping it is somehow) I'd doubt it's the F5 switching to HTTPS. The iRule is only modifying the URI, not the URL which would contain http:// or https://.