Forum Discussion

Leslie_South_55's avatar
Leslie_South_55
Icon for Nimbostratus rankNimbostratus
Nov 06, 2008

HTTP::respond + append URI from HTTP::request

I am using the sample for HTTP::respond 301 from http://devcentral.f5.com/Wiki/default.aspx/iRules/findclass.html

 

 

I need to be able to grab anything after the host and append this to the 3rd row in my classfile

 

 

So if the request was for

 

 

http://host1.domain.com/foo

 

 

and my class file has this

 

 

"host1.domain.com 302 https://securehost.domain.com"

 

 

I want to send

 

https://securehost.domain.com/foo

 

 

to the client. The data after / in the request could be anything and everything, including multiple directories..a request for http://host1.domain.com/foo/bar/foobar/ would need to go to https://securhost.domain.com/foo/bar/foobar/

 

 

Thanks,

 

-L
  • Hi lsouth,

    I am not sure why you want to use the HTTP::respond for achieving this, this can be acheved in a very simple format. Before I go ahead and type the iRule here, for the benefit of the newbies that might stumble across this post here are a few basics

    Take a URL of a website, say iam trying to download chrome from Google, My adress bar will show

    http://www.google.com/chrome

    In this there are 3 parts

    1. "http://" --> This is the protocol, other examples that you would see are https or ftp

    2. "www.google.com" --> This is hostname of the ser ver you are trying to access (known as host)

    3. "/chrome" --> This is URI or Uniform Resource Identifier, it refernces the location inside the server.

    This is how we find our way in the websites

    To reference all this in iRules, you can use the following tags [HTTP::host] and [HTTP::uri]

    Please be informed that "/" is a part of URI, known as the root location. So when you try to access the host without an URI, your browser automatically appends the "/"

    So for your case we can try and use the HTTP::redirect feature, as it does the 302 redirection as you want it

    Use this iRule

    
    when HTTP_REQUEST {
    if {[HTTP::host] equals "host1.domain.com" } {
    HTTP::redirect https://securehost.domain.com[HTTP::uri]
    }
    }
    

    This will redirect your request and append your URI

    so if anyone tries to access http://host1.domain.com/foo/foo/foo.bar he/she gets redirected to https://securehost.domain.com/foo/foo/foo.bar

    Hope this helps
  • The 301 vs. a 302 is the request of the client, and to my understanding, HTTP::respond is the only way to issue a 301. Using the example I can append anything to the URI, but I get a double slash in the request

    Here is the rule I am using (complements of the devcentral examples)

     
     when HTTP_REQUEST { 
         Check if there is a class entry which starts with the requested URI 
        set row [findclass [string tolower [HTTP::host]] $::host_redirects] 
         Check if there was a matched row 
        if { $row ne "" }{ 
     log local0. "Matched $row" 
            Send a response using the status and location from the class 
           HTTP::respond [getfield $row " " 2] Location [getfield $row " " 3][HTTP::uri] Connection Close 
            Clear the row variable 
           unset row 
        } 
     } 
     

    The request was to http://host1.domain.com/foo

    the 301 sends the request to

    https://securehost.domain.com//foo

    Is there a way to remove the double slash from this line, as it is sending the entire URI

    HTTP::respond [getfield $row " " 2] Location [getfield $row " " 3][HTTP::uri] Connection Close

    Thanks

    -L

  • UPDATE

     

    I had a trailing / in my classfile at the end of the new location, so I removed it since the logic is already doing that for me. It seems to work.

     

     

    Thanks

     

    -L
  • If the third field in the class don't have a trailing slash, then you shouldn't see a double slash in the redirect unless the requested URI had two slashes

     

     

    "host1.domain.com 302 https://securehost.domain.com"

     

     

    Can you check that https://securehost.domain.com doesn't have a trailing slash?

     

     

    If it doesn't and you still see two slashes, can you add logging of each component of the redirect?

     

     

    log local0. "HTTP::respond \[getfield $row " " 2\] Location \[getfield $row " " 3\]\[HTTP::uri\] Connection Close:\

     

    HTTP::respond [getfield $row " " 2] Location [getfield $row " " 3][HTTP::uri] Connection Close"

     

     

    Aaron
  • Oh great,

     

     

    Thats good news . I was just wondering as to what the client is trying to achieve with a 301. I normally see them done in 3DNS architecure.

     

  • A 301 response is used for permanent redirects:

     

     

     

    ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt

     

     

    10.3.2 301 Moved Permanently

     

     

    The requested resource has been assigned a new permanent URI and any

     

    future references to this resource SHOULD use one of the returned

     

    URIs. Clients with link editing capabilities ought to automatically

     

    re-link references to the Request-URI to one or more of the new

     

    references returned by the server, where possible. This response is

     

    cacheable unless indicated otherwise.

     

     

    The new permanent URI SHOULD be given by the Location field in the

     

    response. Unless the request method was HEAD, the entity of the

     

    response SHOULD contain a short hypertext note with a hyperlink to

     

    the new URI(s).

     

     

     

     

    Aaron