Forum Discussion

Ethan_West_1101's avatar
Ethan_West_1101
Historic F5 Account
May 01, 2008

301 to Redirect

Hi All,

 

 

I am trying to get an iRule working that will redirect on a 301 to the Specific Host URI requested. This is for an application that users request is without the trailing slash (http://www.somesite.com/stcokquotes) in turn the servers respond with 301.

 

 

My question how do I write the iRule to respond with a 302 for example and preserve the Host and URI? I am looking at a previous post that works for me but is inserting the Header Value Location from the server which obviously breaks the app. How do I preserve the Host / URI in the inital request.

 

 

when HTTP_RESPONSE {

 

Check if the server response is a 302

 

if {[HTTP::status] == 301}{

 

Send a 301 response to the client with the Location header value from the server's response HTTP::respond 302 Location [HTTP::header value Location]

 

}

 

}

 

Thanks for the help in advance.

7 Replies

  • Can you clarify this a bit/add an example or two? What do you want to rewrite in the app's response? What do you want to rewrite it to?

     

     

    Does the client make a request to http://www.example.com/some/path, and the app responds with a 301 to http://someother.example.com/some/path? Do you want to then rewrite the app's redirect to something different?

     

     

    Thanks,

     

    Aaron
  • Ethan_West_1101's avatar
    Ethan_West_1101
    Historic F5 Account
    Hi Hoolio,

     

     

    Yes that is exactly the case. The client makes a request like http://wwww.somesite.com/stockquotes which omits the trailing slash. The app then responds with a 301 with another internal path like http://www.somesite.corp/some/path which in turn breaks when accessing it say from the outside. It bascially is an issue when the trailing slash is included in the request the response is fine but when it is not which is the case 95% of the time the app responds with a 301 and it's broken.

     

     

    How do I inlcude in the iRule to preserve the requested host/uri in the response to look like

     

     

    http://www.somesite.com/stcokquote - response on 301 http://www.somesite.com/stockquote/.

     

     

    Also FYI the rewrite redirect option in HTTP Profile does not work but I kind of expected that.

     

  • If you always want to use the same domain in the redirect, you can set it statically. Else, if it needs to change dynamically according to what the client requested, you'd need to save the host header value on every request to use in rewriting the redirects. If you know the string you want to search for it would be more efficient to hardcode that as well.

     

     

    Do you know the strings you want to search for and replace, or do you need to parse these dynamically from the client request and the app's redirect response?

     

     

    Aaron
  • Ethan_West_1101's avatar
    Ethan_West_1101
    Historic F5 Account
    Yes I do know the strings they will always be the same. In fact it's only going to be one per Virtual Server.

     

     

    Any thoughts?
  • If it's static strings, the change is pretty simple. Just look in responses for a 30x status, then set the Location header to a value with the app's host replaced with the outside host name.

    
    when HTTP_RESPONSE {
        Check for redirects
       if {[HTTP::is_redirect]}{
           Log a debug message.  Comment this out when done testing.
          log local0. "[IP::client_addr]:[TCP::client_port]: Updating Location header from [HTTP::header value Location], \
             to: [string map -nocase {find replace} [HTTP::header value Location]]"
           Replace the Location header with the new host name
          HTTP::header replace Location [string map -nocase {find replace} [HTTP::header value Location]]
       }
    }

    Replace 'find' with the string you want to search for and 'replace' with the string you want to replace it with.

    Aaron
  • Good to hear. Make sure to comment out the log statement to save resources, once you're done testing.

     

     

    Aaron