Forum Discussion

Rogerio_Teixeir's avatar
Rogerio_Teixeir
Icon for Nimbostratus rankNimbostratus
Apr 10, 2008

Replace host based on file type

Hi,

 

 

Im trying to creat a irule that replace de hostname (FQDN) based on what fily type is request...

 

 

Like this:

 

 

Based on class images (jpg,gif,png...etc)

 

 

client request for :www.foobar.com/img/nonono.jpg

 

 

replace the hostname

 

 

Server respond: images.foobar.com/img/nonono.jpg

 

 

???

 

 

 

 

Regards,

 

Rogerio
  • Chris_Seymour_1's avatar
    Chris_Seymour_1
    Historic F5 Account
    Rogerio,

    If I understand correctly, you want to replace the host www.foobar.com with images.foobar.com.

    For one file type you could do something simple like this:

    
    when HTTP_REQUEST {
        if { [HTTP::path] ends_with ".jpg" } {
          HTTP::redirect "http://images.foobar.com[HTTP::uri]"
        }
       }

    For multiple images types you should use a data group with matchclass for your images like this:

     
    class images {
      ".bmp"
      ".gif"
      ".jpg"
      ".pdf"
      ".BMP"
      ".GIF"
      ".JPG"
      ".PDF"
     }
    when HTTP_REQUEST {
        if { [matchclass [HTTP::path] ends_with $::images] } {
          HTTP::redirect "http://images.foobar.com[HTTP::uri]"
        }
       }

    Hope this helps.

    Chris
  • Hi,

     

     

    I like to do this over HTTP_RESPONSE. This is possible?

     

    And i need to preserve my existing cookies.....

     

     

    Regards,

     

    rogerio
  • Hi,

     

     

    Its is possible the rewrite the host address based in "Content Type"?

     

     

    Regards,

     

    Rogerio
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Hi Rogerio,

    Something like this?

    
    when HTTP_RESPONSE {
        Check if the Content-Type header value matches our criteria
       if {[HTTP::header value Content-Type] contains "somestring"}{
          log local0. "[IP::client_addr]:[TCP::client_port]: Matched the Content-Type check for [HTTP::header value Content-Type]"
           Initialize a variable to save the cookies
          set cookie_string ""
           Loop through each Set-Cookie header.  Save the value to a string, and remove the header.
          while {[HTTP::header exists Set-Cookie]}{
              Append the current Set-Cookie value to the cookie string
             append cookie_string [HTTP::header value Set-Cookie]\;
             log local0. "[IP::client_addr]:[TCP::client_port]: Current \$cookie_string: $cookie_string"
              Delete the current Set-Cookie header
             HTTP::header remove Set-Cookie
          }
           Include the cookies in the 302 response if there were any
          if {[string length $cookie_string]}{
             log local0. "[IP::client_addr]:[TCP::client_port]: Redirecting with cookies to: http://example.com Set-Cookie $cookie_string"
              Send the 302 redirect response with the cookies
             HTTP::respond 302 Location "http://example.com" Set-Cookie $cookie_string
          } else {
             log local0. "[IP::client_addr]:[TCP::client_port]: Redirecting without cookies to: http://example.com"
              Send the 302 redirect response without the Set-Cookie header as there weren't any cookies
             HTTP::respond 302 Location "http://example.com"
          }
       }
    }

    I didn't test it, but it passed an iRuler syntax check. If you run into problems with it, check the /var/log/ltm log file for debug output. Make sure to comment out or remove the logging before using the iRule in production.

    Aaron