Forum Discussion

tidenz_92110's avatar
tidenz_92110
Icon for Nimbostratus rankNimbostratus
Mar 24, 2010

Location rewrite redirect 302 string issues

New to string commands and irules but i am trying to do a header replace without using regex.

 

 

In the redirect location header I am trying to strip out the protocol and the hostname to make the redirect relative to the application for testing

 

(yes i know the RFC is to have a FQDN in the location header) but the apps guys want it.

 

 

So the current location header looks like

 

 

http://company.com/app1/sad.do

 

 

i want

 

 

/app1/sad.do

 

 

note this will be used on multiple domains so it maybe company1 or company2 etc.

 

 

I have tried to read it into a variable and then use a findstr to find the first trailing slash but i keep matching the / in "http:/" is there a way to miss the first 2 // and find the last /

 

 

The irule is as follows

 

 

when HTTP_RESPONSE {

 

set newloc [HTTP::header Location]

 

log local0.debug "$newloc"

 

set rewriteloc [findstr $newloc "/" 9 ]

 

log local0.debug "$rewriteloc"

 

if { [HTTP::is_redirect] }{

 

HTTP::header replace Location "$rewriteloc"

 

}

 

 

Thanks

 

2 Replies

  • Hi Tidenz,

    You can use scan for this:

      
      when HTTP_RESPONSE {  
        
          Check if response is a redirect  
         if { [HTTP::is_redirect] }{  
        
             Use scan to check for http://$host/$uri, saving the matches  
             the leading / of the URI will be trimmed, so we need to append it in the Location rewrite 
            if {[scan [HTTP::header Location] {http://%[^/]/%s} host uri] == 2}{  
               HTTP::header replace Location "/$uri"  
            }  
         }   
      }  
      

    This assumes that the Location value always starts with http:// and has at least one character after the third slash in http://www.example.com/. If it could be https:// also, let me know and I'll try something else.

    Aaron
  • Thanks for the prompt response hoolio, i am not aware of the redirects including https but knowing the dev's it maybe a option later down the line. I will run with the current irule for now.

     

     

    Cheers