Forum Moderators: phranque

Message Too Old, No Replies

Having a rewrite ignore "%2F" but not "/"?

         

Wilco

8:22 pm on May 2, 2008 (gmt 0)

10+ Year Member



I've got a rewrite that translates "http://www.mydomain.com/search/category/query/page" to "search.php?c=category&q=query&p=page"

Obviously I can't have a search query with a "/" in it, but if I use PHP's urlencode() function, I can have it convert it to the "%2F" hex value.

The problem is, my .htaccess file is still interpreting "%2F" as a straight "/". Is there a way around this? Here's the line I've got right now:


RewriteRule^search/([A-Za-z0-9-]*)/?([A-Za-z0-9\+\%-]*)/?([0-9-]*)/?$ search.php?q=$2&c=$1&p=$3 [L]

jdMorgan

2:13 am on May 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you're over-complicating this. The problem is that your pattern is too 'accepting' in one way, and not accepting enough in the "q" part.

How about using this instead:


RewriteRule^search/([A-Za-z0-9\-]+)/([A-Za-z0-[b]9/\+[/b]\%\-]+)/([0-9\-]+)/?$ search.php?q=$2&c=$1&p=$3 [L]

This takes the first slash-delimited URL-path-part and puts it in as the "c" value. It takes the last URL-path-part and puts it in as the "p" value. It then takes whatever is left in the middle and uses that for the "q" value.

If that won't work in your application, then you'll need to use a RewriteCond to examine %{THE_REQUEST} to get the non-unencoded (original) client request header -- The URL-path 'seen' by RewriteRule is unencoded, which is why it sees "%2f" and "/" as identical. %{THE_REQUEST} is of the form:

GET /search/widgets/furry/green/2 HTTP/1.1
-- just as it appears in your raw server logs.

Using %{THE_REQUEST} you'd see the "/" characters and "%2f" character sequences in the URL-path.

Jim