Forum Moderators: phranque

Message Too Old, No Replies

Trouble with RewriteCond and RewriteRule

         

kizamps

6:47 pm on Feb 22, 2007 (gmt 0)

10+ Year Member



Having trouble with a RewriteCond and Rewrite Rule.

I'm trying to rewrite anything that has

shop.php?N=[number]+[number]

to

simply [number]+[number].php and its not working...here's my example:

/folder/shop.php?N=3+20

I want it to rewrite to:

/folder/3+20.php

RewriteCond %{THE_REQUEST}%{QUERY_STRING} ^(.*)shop.php\?(.*)N\=(([0-9.]+\+?){6})(.*)$ [NC]
RewriteRule ^(.*)/shop\.php$ /$1/%3\.php\?%5 [R=301,L]

Any ideas on why it won't work?

jdMorgan

9:37 pm on Feb 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you didn't say anything about how "this does not work" -- how you tested and how the results you got compared to what you expected, I can't offer any solution.

However, the variable %{THE_REQUEST} will look like this:

GET /folder/shop.php?N=3%2b20 HTTP/1.1

Therefore, your RewriteCond pattern needs to take that into account, since I doubt that you want that "HTTP/1.1" on the end of your new query string.

I should also warn you that while "+" is a valid character in a query string, it is not valid in a URL. Therefore, it will be escaped (hex-encoded) before being sent by the client to your server, and will appear as /folder/3%2b20.php in search results. See RFC2396 Uniform Resource Identifiers (URI): Generic Syntax [faqs.org] for more information.

I would also caution you that the use of multiple ".*" patterns leads to very poor performance in regular-expressions processing. You would do well to use specific or negative pattern-matching to avoid this.

Perhaps something more like this:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*shop\.php\?(([^&]+&)*([^&]+)&)?N=(([0-9.]+\%2b){5}[0-9.]+)(&[^\ ]+)?\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)/shop\.php$ /$1/%4\.php\?%2%3%6 [R=301,L]

Jim