Forum Discussion

mguned_60772's avatar
mguned_60772
Icon for Nimbostratus rankNimbostratus
Feb 19, 2010

URL Rewrite findstr

Trying to do the following:

 

 

Redirect -

 

 

https://oldsite.com/xxx/xxx/xxx.asp?firststring=FIRSTxxxx_xxxxsecondstring=SECOND

 

 

to -

 

 

http://newsite.com/xxx.aspx?xxxxxx=FIRSTxxxxx=SECOND

 

 

Here is what I have tried to no avail:

 

 

elseif { [HTTP::uri] contains "/xxx/xxx/xxx.asp" } { set FIRST [findstr [HTTP::uri] "xxx" 4 ";"] set SECOND [findstr [HTTP::uri] "xxx" 6 ";"] HTTP::redirect "http://newsite.com/xxx.aspx?xxx=$FIRSTxxx=$SECOND"}

 

 

Any help is much appreciated!

 

Cheers!

 

 

 

 

 

  • Normally I'd suggest using URI::query to parse the parameter values and then insert them in the redirect. However, there is a bug in the URI::query command (described in this post Click here).

    So you could try something like:

     
     elseif { [HTTP::path] eq "/xxx/xxx/xxx.asp" } { 
      
         Parse the value of param_one from the URI 
        if {$query starts_with "param_one="}{  
           set FIRST [findstr $query "param_one=" 10 &] 
           log local0. "\$FIRST: $FIRST" 
        } else {  
            Parameter wasn't at the start of the query string, so check for &param_one= in the query string 
            Need to hardcode the length of the parameter name + 2 for findstr 
           set FIRST [findstr $query "&param_one=" 11 &] 
           log local0. "\$FIRST: $FIRST" 
        } 
         Parse the value of param_2 from the URI 
        if {$query starts_with "param_2="}{ 
           set SECOND [findstr $query "param_2=" 8 &] 
           log local0. "\$SECOND: $SECOND" 
        } else { 
            Parameter wasn't at the start of the query string, so check for &param_2= in the query string 
            Need to hardcode the length of the parameter name + 2 for findstr 
           set SECOND [findstr $query "&param_2=" 9 &] 
           log local0. "\$SECOND: $SECOND" 
        } 
      
        HTTP::redirect "http://newsite.com/xxx.aspx?xxx=$FIRSTxxx=$SECOND"}  
     

    Aaron
  • Thanks for the info...working on this now. BTW, if you could do a URI::query ...what would it look like?

     

     

    -Thanks!
  • You can use [string range $string 0 3] to get a substring from $string. If you want help, can you post a more exact anonymized example of the URI you're trying to parse?

     

     

    Also note that Spark gave an example of how to fix the URI::query parsing:

     

     

    [URI::query "?&[HTTP::query]" &param_1]

     

     

    You need to prepend a & character to the name of the parameter in order for this to work.

     

     

    Aaron
  • I am still having trouble extracting the 4 characters associated with mailDrop ... it seems to grab everything behind mailDrop= and not just the 4 characters.
  • Sorry.. typing faster than I was reading. The URI::query command should return whatever is between mailDrop= and the next & or the end of the string, regardless of the length of that value. Are you wanting to only use the first four characters of the mailDrop parameter value even if it's longer in some cases? Do you see a & in the redirect string (or what exactly do you see in the string and is that what the mailDrop parameter value is set to in the original URI)?

     

     

    If you do want to take only the first four characters of the mailDrop parameter value, you can use this:

     

     

    HTTP::redirect "http://newsite.com/buy.aspx?Quantity=0&CouponCode=[string range [URI::query "?&[HTTP::query]" &mailDrop] 0 3]"

     

     

    Aaron
  • So here is what I have:

     

     

    when HTTP_REQUEST {

     

    if { [HTTP::uri]contains "/bookstore/checkout/mailingSeOrderEntry.asp" } {

     

    set extract [findstr [HTTP::uri] "mailDrop=" 9 "&" ]

     

    HTTP::redirect "http://newsite.com/buy.aspx?Quantity=0&CouponCode=extract$"}

     

    }

     

     

    What is happening is that the findstr is appending everything behind mailDrop and not just the 4 characters that are needed. I also wanted to mention that it will always be only 4 characters.
  • Can you try this then:

     

     

    HTTP::redirect "http://newsite.com/buy.aspx?Quantity=0&CouponCode=[string range [URI::query "?&[HTTP::query]" &mailDrop] 0 3]"

     

     

    Aaron
  • Sorry, I missed that on your earlier response and yes it works! Thanks again for the help...much appreciated.