Forum Discussion

Joe_Pipitone's avatar
Joe_Pipitone
Icon for Nimbostratus rankNimbostratus
Mar 02, 2011

Regex

I've been requested to perform a rewrite / redirect which does the following:

 

 

http://subdomain.ourcompany.com/webext/01.aspx?status=error (pass the 01 over to the url below and say id=thatNumber)

 

 

needs to rewrite to

 

 

http://subdomain.ourcompany.com/post/post.ashx?id=01&status=error

 

 

I'm trying to make sure I'm going in the right direction here by using regex to analyze the URI, however the 01.aspx will be a different number, it will not always come from the application that way.

 

 

The web application may spit out a different error number such as 45.aspx or 123.aspx. The web application increments this number based on the number of errors received within the application, so there's a possibility that we will get into triple, 4, 5, 6 digits in the future.

 

 

The only thing that changes in the uri is this number. The ?status=error needs to be appended and the number moved over to result in something like:

 

 

http://subdomain.ourcompany.com/post/post.ashx?id=01&status=error

 

 

I found this iRule - can I use this or something similar to increment using \d+ to say one or more digits within the regex?

 

 

Or a string match?

 

 

string match -nocase {[0-9999][[0-9999]}

 

 

Thanks for any help - I'm unsure how to move forward.

 

 

elseif {[HTTP::host] == "subdomain.ourcompany.com" } { if { [HTTP::uri] contains "/webext/\d+.aspx?status=error"} {

 

set urivar

 

if { ! ( $urivar matches_regex ^.*?

 

\/webext\/\d+\[a-zA-Z0-9]) } {

 

rewrite here

 

}

 

else {

 

pool something else

 

}

 

}

 

 

  • This ended up working:

     

     

    HTTP::redirect "/post/post.ashx?id=${id}&[HTTP::query]"

     

     

    It does append the ampersand - any thoughts on how to remove that if its not needed?

     

     

    http://server/webex/234234.aspx

     

     

    Becomes this:

     

     

    http://server/post/post.ashx?id=234234&*

     

     

  • You could add a check to see if the query string is null, but it's extra work with no benefit (unless the extra ampersand breaks the app).

    when HTTP_REQUEST {
    
        Check if URI starts with /webex/
       if {[string tolower [HTTP::uri]] starts_with "/webex/"}{
    
           Parse 000file.ext from /path/to/000file.ext and save the leading digits to the $id variable
           where 000 is one or more digits and file.ext is any string
          scan [URI::basename [HTTP::uri]] {%[0-9]%s} id rest
          if {$id ne ""}{
             log local0. "Scanned $id from $uri"
     if {[HTTP::query] eq ""}{
                HTTP::redirect "http://[HTTP::host]/post/post.ashx?id=${id}"
             } else {
                HTTP::redirect "http://[HTTP::host]/post/post.ashx?id=${id}&[HTTP::query]"
             }
          }
       }
    }
    

    Aaron