Forum Discussion

Joe_Greco_BBB_1's avatar
Joe_Greco_BBB_1
Icon for Nimbostratus rankNimbostratus
Jan 15, 2015

Search URI for ?

I have a customer that wants to capture a user entering a "?" into the URI as a search string, and perform a redirect on it. I've tried several ways to capture this, but none seem to work properly.

 

The users would enter something like: http://mysite.com/?freeshipping I want to find the /? and do a redirect to http://mysite.com/store/s/freeshipping

 

I've included one of my attempts with the hope that someone has already figured this out?

 

Here's what I came up with:

 

when HTTP_REQUEST { if { [HTTP::uri] starts_with "/?" } { HTTP::redirect "http://[HTTP::host]/store/s/[HTTP::uri]" } else { HTTP::redirect "http://[HTTP::host][HTTP::uri]" } }

 

Any help would be greatly appreciated!

 

3 Replies

  • That should work to some degree. If a request came in for http://mysite.com/?freeshipping it would get redirected to (notice the double slash '//' as you had /store/s/[HTTP::uri when you are probably wanting /store/s[HTTP::uri]

     

    If the URI does not start with /? it will redirect back to the same location they requested, which will cause a permanent redirect loop. You should remove the else portion of the iRule.

     

  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus

    This should do it:

    when HTTP_REQUEST {
    
        if { [HTTP::uri] starts_with "/?" } {
    
            HTTP::respond 301 Location "http://[HTTP::host]/store/s/[HTTP::query]"
    
        }
    
    }
    

    (edited on 2015-01-22 to reflect Eric's fix)

  • Arie,

    That didn't quite work as it never matched the ?. However, I tried a slight modification and it does work. Instead of using HTTP::path, I'm using HTTP::uri and it does match and redirect as I need. In case anyone else is following, here is the rule that I'm currently using:

    when HTTP_REQUEST {

    if { [HTTP::uri] starts_with "/?" } {
    
        HTTP::respond 301 Location "http://[HTTP::host]/store/s/[HTTP::query]"
    
    }
    

    }