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: Website Hosting - Mysite.com 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!

That should work to some degree. If a request came in for Website Hosting - Mysite.com 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.

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]"

}

}