Forum Discussion

Annsar_Akhtar's avatar
Annsar_Akhtar
Icon for Nimbostratus rankNimbostratus
May 22, 2014

irules to rewrite old url to new url without changing the users browser url

Hi All

 

I am looking at the best way of performing a rewrite using an Irule, I had a google and found some examples and I haven't been able to get any to really work as expecting.

 

What I am trying to achieve is our devs are wanting to preserve the users browsers url (i.e. http://oldsite.test.com/test-site) but redirect the user to an alternate url (i.e. http://newsite.test.com/alternatesite/test) which is hosted via the same F5 but in a different pool.

 

I thought something like the below would achieve something along the lines but any pointers or suggestions would be extremely helpful:

 

when HTTP_REQUEST { if { [HTTP::uri] starts_with "/test-site" }{ HTTP::header replace "Host" "newsite.test.com" HTTP::path "/alternatesite/test" pool newsite-pool-HTTP } else { pool oldsite-pool-HTTP } }

 

Thanks in advance

 

7 Replies

  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus

    HTTP::uri is probably your best bet. It retains the URL in the browser's address bar but the LTM will send the re-written URL to the nodes.

     

    Documentation: https://devcentral.f5.com/wiki/iRules.HTTP__uri.ashx

     

  • Reformatting your iRule for discussion:

    when HTTP_REQUEST { 
        if { [HTTP::uri] starts_with "/test-site" } { 
            HTTP::header replace "Host" "newsite.test.com" 
            HTTP::path "/alternatesite/test" 
            pool newsite-pool-HTTP 
        } else { 
            pool oldsite-pool-HTTP 
        } 
    }
    

    What you're basically saying is if the URI starts with /test-site, change the Host header, change the HTTP path, and send to a different pool. Otherwise send to the oldsite pool. I would start by changing HTTP::path to HTTP::uri. The HTTP::path is the part of the URI before any query strings, while HTTP::uri is the entire uri. The above iRule will work for the simplest environments, as long as the new site only ever uses the /alternatesite/test URI. If it doesn't, and it's in any way dynamic, then you have to do some more work. Depending on the complexity, you may want to take a look at the ProxyPass iRule (v11.3 and below) or the rewrite profiles in 11.4 and above.

    You may also want to consider enabling a OneConnect profile.

  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus

    Thanks for reformatting the code, Kevin - I hadn't noticed the HTTP::path...

     

    Regarding the Host header, it may not be necessary to replace it. Depends on the configuration of the web server.

     

  • Thanks Arie and Kevin, we are currently on v11.2.1 HF10 so will try the option of changing the path to a URI, unfortunately the URI is going to be a dynamic path but wanted to look at getting a simple rewrite working initially as a proof of concept but guess we might have to look into bringing our F5s up to at least v11.3 in order to try the ProxyPass irule option.

     

  • Thanks for that, I will give that a try I found the following article: https://devcentral.f5.com/wiki/iRules.proxypassv10.ashx so will give it a read and try

     

  • Have you considered using a HTTP_class for this purpose. Using a HTTP class you can specify using Pattern strings and apply an action such as Redirect to URL or select another pool.

     

  • Thanks, I am open to suggestions I am looking at ProxyPass iRule at the minute and suspect I haven't configured my data set correct as the request from the logs look like they are trying to redirect but back to the originating pool.