Forum Discussion

Tony_105617's avatar
Tony_105617
Icon for Nimbostratus rankNimbostratus
Apr 18, 2013

need a rewrite or redirect rule for URI

I have only done a little iRule scripting so any thing would be helpful.

 

 

I have a customer that wants to have a iRule that will rewrite the uri portion of a request.

 

here's what they are looking for.

 

If the request comes in for http://www.abc.com/browse.php it would become http://www.abc.com/addeduri/browse.php

 

basically if the request comes in without the added uri change it to with the new uri added in the path.

 

6 Replies

  • Changing the incoming URI request, without causing a redirect, it pretty straight forward with the HTTP::uri command. Here's an example:

    
    when HTTP_REQUEST {
         if { not ( [string tolower [HTTP::uri]] starts_with "/addeduri" ) } {
              HTTP::uri "/addeduri[HTTP::uri]"
         }
    }
    

    This will cause any incoming URI request, even "/", to have the "/addeduri" string bolted to the front of it.
  • will that also add whatever uri if there is one to the end of the request?
  • Yes. The HTTP::uri command reads and writes, depending on context.

     

     

    So HTTP::uri "/addeduri[HTTP::uri]" means to write a new URI with the "/addeduri" string and the existing URI, read from [HTTP::uri]. This would cover ANY URI that doesn't start with "/addeduri", so if the user requests "/", it will be translated to "/addeduri/".
  • The HTTP::uri command only rewrites the URI on ingress flow. In order to see the new URI in the browser address bar you actually have to cause a redirect event, which is to say that you force the user to go to a new URL. I assumed from your original request that you were looking to just change the incoming URI, which the previous iRule will do. The browser won't see it, but the server will. If, however, you need the client to see the new address, then change out the HTTP::uri write command in the above iRule with the HTTP::redirect command:

    
    when HTTP_REQUEST {
         if { not ( [string tolower [HTTP::uri]] starts_with "/addeduri" ) } {
              HTTP::redirect "/addeduri[HTTP::uri]"
         }
    }
    

    The browser will make its original request, let's say "/browse.php". The iRule will see that the URI doesn't start with "/addeduri", so it will issue a 302 redirect to the client and a Location header that includes "/addeduri" and the originally requested URI (ex. "/addeduri/browse.php").