Welcome to WebmasterWorld Guest from 54.144.48.252

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess won't catch rewrite URL's that include a % character

how to make htaccess URL's that include a % character

     
9:09 pm on Jan 31, 2011 (gmt 0)



some of my URL's include query lines and some sites link to them replacing the ?, = and + with their % encoding, which means an URL like this:

http://www.example.com/exampleurl.php?item=this+item

ends up like this:
http://www.example.com/exampleurl.php%3Fitem\%3Dthis\%2Bitem

Result=
My server doesn't find them and Google marks them as 404.

No matter how I try to catch them for redirection, it doesn't work. I tried these, without success:

RewriteRule ^exampleurl.php\%3Fitem\%3D(.*)\%2B(.*)$ /index.php [R=301,L]

RewriteRule ^exampleurl.php%3Fitem%3D(.*)%2B(.*)$ /index.php [R=301,L]

RewriteCond %{QUERY_STRING} ^item%3D(.*)$
RewriteRule ... as usual...

Of course the last one can't work since the "?" isn't even in the url...

Does anybody have a solution? I've search the forums but don't even know exactly how to refer to the % special characters in my search terms.

[edited by: jdMorgan at 7:20 pm (utc) on Feb 7, 2011]
[edit reason] example.com to suppress auto-linking. [/edit]

10:43 am on Feb 1, 2011 (gmt 0)



You don't actually need any escaping other than for the equals symbol which is an operator. Additionally, if you're trying to match the part after the question mark, you must use a rewrite condition, e.g.


RewriteCond %{QUERY_STRING} ^item=this\+item
RewriteRule .* http://www.example.com/newurl.php [R=301,L]
11:10 am on Feb 1, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The above code will re-append the original query string.

To suppress that action add a question mark to the target URL.

If you want to "re-build" the query string with ? and = the code will be a LOT more complex.

There was a similar question a few weeks ago. That other thread contains a large amount of example code.
7:24 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member




# Replace encoded "?" characters with literal "?" characters
RewriteCond %{THE_REQUEST ^[A-Z]+\ /([^%]*)\%(25)*3[fF]([^\ ]+)\ HTTP?
RewriteRule ^ http://www.example.com/%1?%3 [R=301,L]

The (25)*3[fF] allows for multiply-encoded question marks, encoded using upper- or lower-case "F".

Jim
 

Featured Threads

Hot Threads This Week

Hot Threads This Month