Forum Discussion

samigo_81875's avatar
samigo_81875
Icon for Nimbostratus rankNimbostratus
Sep 03, 2013

Search replace uri

Hi,

I want to search replace using regex

/content/learning//production

TO

/content/learning/production

Tried below but the regex doesn't work

 if { [URI::decode [string tolower [HTTP::uri]]] matches_regex "/content/learning/(.*)/production" } {
    set replaceURI [string map [list "learning/(.*)/production" "learning/production" ] [HTTP::uri]]    
    log local0. "REPLACE --> $replaceURI"  
    HTTP::respond 307 noserver Location "https://myserver.com$replaceURI" 
  }

Appreciate your help

2 Replies

  • I think I got it working with below

     

    if { [URI::decode [string tolower [HTTP::uri]]] matches_regex "/content/learning/(.)/production/frshdb" } { regsub {learning/(.)/production} [HTTP::uri] "learning/production" replaceURI HTTP::respond 307 noserver Location "https://myserver.com$replaceURI" }

     

  • I'd try string commands instead of regex to save some CPU cycles:

     

    when HTTP_REQUEST {
       if {[HTTP::path] matches_glob "/*//*"}{
          HTTP::respond 307 noserver Location [string map {// /} [HTTP::uri]]
       }
    }

    Also are you sure you want to use a 307?

     

    "...since many pre-HTTP/1.1 user agents do not understand the 307 status."

     

    http://tools.ietf.org/html/rfc2616section-10.3.8

     

    The temporary 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) , since many pre-HTTP/1.1 user agents do not understand the 307 status. Therefore, the note SHOULD contain the information necessary for a user to repeat the original request on the new URI.

     

    If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued. Aaron