Forum Discussion

Annsar_Akhtar's avatar
Annsar_Akhtar
Icon for Nimbostratus rankNimbostratus
Sep 21, 2012

Irules redirect/rewrite query

Hi all

 

I am new to the irules forum, and have been looking for the best way to handle a request come from our development team.

 

The request is as following, currently users browse to http://www.test.com/destination/xyz and would like to redirect users based on capturing the last bit of the url i.e xyz to http://www.test2.com/destination/xyz

 

So is it possible to capture the last part of a url with an irule and then pass this to a set url and append the capture part of the url, to add some more complication we then need to rewrite the url to the original for SEO purposes to maintain rankings.

 

Our f5’s are currently running 11.20. Build 2451.3, the servers hosting the sites are both in separate pools.

 

Thanks in advance

 

6 Replies

  • Not 100% clear on your requirements here, but guessing you need to redirect users from www.test.com to www.test2.com IF the end of the URL contains "/xyz"? If so:

    
    when HTTP_REQUEST {
        if { ( [string tolower [HTTP::host]] equals "www.test.com" ) and ( [string tolower [HTTP::uri]] ends_with "/xyz" ) } {
            HTTP::redirect "http://www.test2.com[HTTP::uri]"
        }
    }
    

    The URI is left intact, so we're just changing the host value in the redirect. Is this what you're looking for?

  • Hi Kevin

     

     

    Thanks for that, the requirement is to capture the last part of the url (which could be anything, i.e /...) and then pass this to the new url, http://www.test2.com/... also to rewrite it back to the orginal url to maintain SEO.

     

     

    I can see the irule submitted will allow me to redirect based on /xyz but we ideally need to be able to capture and pass this on, if its not possible then we will have to create a long list with if else to match.

     

     

    Thanks

     

  • Search the various iRule samples linked under the wiki entry for HTTP::uri

     

     

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

     

     

    You can use various functions, or just plain string functions, to disect the URI and extract any part of it. You could then use it in compsing a pool name, or recreating a new URI and so on....

     

     

    Thanks,

     

    Mohamed.
  • How about something like this then:

    
    when HTTP_REQUEST {
    if { [string tolower [HTTP::host]] equals "www.test.com" } {
            
    sample http://www.test.com/destination/xyz
    
    set firstpart [string range [HTTP::uri] 0 [expr [string last "/" [HTTP::uri]] -1]]
    set secpart [string range [HTTP::uri] [string last "/" [HTTP::uri]] end]
    
    log local0. "firstpart = $firstpart"
    log local0. "secpart = $secpart"
    
    HTTP::redirect "http://www.test2.com$firstpart$secpart"
        }
    }
    

    What do you mean by " also to rewrite it back to the orginal url to maintain SEO"?

  • Thanks, I will have a look at the wiki and have a play with the code sample. The page is used for SEO so any redirect away from the page would affect the rankings so would like to maintain the orignal URL to end users browser after redirecting them but not sure if this going to be possible.
  • For the purposes of SEO, I'm guessing you'd need to maintain the original host value (www.test.com). Unless I'm still not understanding, how can you maintain SEO for www.test.com if you're physically redirecting users to www.test2.com? What's important - the host value or URI?

     

     

    Do www.test.com and www.test2.com live on the same BIG-IP? Can you simply send www.test.com to the pool of servers for www.test2.com (and change the Host header if necessary)?