Forum Discussion

elvisc_26948's avatar
elvisc_26948
Icon for Nimbostratus rankNimbostratus
Dec 21, 2009

Replace URI subset

I am simply trying to look for ch1 and replace that with ch2 in URI... any idea?

 

 

URI example:

 

Convert

 

/abc/cde/ch1/xxx/yyy/file.txt

 

To

 

/abc/cde/ch2/xxx/yyy/file.txt

 

 

 

 

 

when HTTP_REQUEST {

 

 

if { [HTTP::method] contains "GET" } {

 

 

log local0. "========>> HTTP URI: [HTTP::uri]"

 

STREAM::disable

 

STREAM::expression "@ch1@ch2@"

 

STREAM::enable

 

}

 

}

4 Replies

  • Hi Elvisc,

    If the path and depth is relatively the same then you could do the following

      
      when HTTP_REQUEST {  
        if { [HTTP::uri] starts_with "/abc/cde/ch1/" } {  
             set namepath [URI::path [HTTP::uri] 4 5]  
             set basen [URI::basename [HTTP::uri]]  
             HTTP::redirect "http://[HTTP::host]/abc/cde/ch2/$namepath/$basen"  
            } 
      } 
      

    You might need to tweak it as I haven't tested this before i posted it.

    I hope this helps

    Bhattman

  • I figured out a easier and also transparent to the client:

     

     

    HTTP::path [string map {ch1 ch2} [HTTP::path]]

     

     

     

    Redirect would basically abort the previous http request and then the client has to actually hit enter to get to the redirect link...the above would be seemless.

     

     

    thx for your help.

     

     

    ec.
  • Nice work. You could make the search/replace a little more specific by including the directories:

     

     

    HTTP::path [string map {/ch1/ /ch2/} [HTTP::path]]

     

     

    Aaron