homepage Welcome to WebmasterWorld Guest from 184.72.82.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Htaccess rewrite to replace %3F with ?
rewrite: replace %3F with?
Sam234




msg:4347417
 9:56 pm on Aug 3, 2011 (gmt 0)

htaccess rewrite to replace %3F with ?

The problem:

Some scrapers/bots grab search engine results, which they then insert into their site, but search engine results contain links that are url encoded, meaning a query string ? become %3F. If a link to your site contains %3F it will result in a 404 error, as it will make the server search for a directory name ? which cannot exist. Google webmaster tools will then report errors!

My fix, after many false leads:

You have to use THE_REQUEST to catch the string:

After Your:

RewriteEngine on

Add:

# replace %3F with ?
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\%3[Ff](.*)\ HTTP/ [NC]
RewriteRule \.*$ http://www.mysite.com/%1?%2 [R=301,L]


My skills at apache re-writes are limited, but it worked for me, I hope the solution helps others. :)

PS tried posting this elsewhere, but their 'overprotective' system wouldn't accept!

 

g1smd




msg:4347428
 10:11 pm on Aug 3, 2011 (gmt 0)

This isn't a rewrite. This is a redirect - even though it uses a RewriteRule. Confusing isn't it?

The (.*) pattern matches the entire
GET /somefile?somequery=somevalue HTTP/1.1 request. By using two (.*) patterns you are saying that you want to match "everything" into the first (.*) and then match it all again in the second one. This confuses the parser beyond belief. It then has to make tens of thousands of "back off and retry" trial matches until it discovers you only want "some" of the input in the first backreference and "some" of it in the second.

Replace the two (.*) patterns with something more specific, such as
([^\%]+) and ([^\ ]+)

If you specify [Ff] you don't need the [NC] flag to allow "any case" as you have covered both cases by using [Ff]. You could use [F] and [NC] of course.

A redirect such as this should be listed as one of the very first redirects in your list of redirects.

lucy24




msg:4347430
 10:16 pm on Aug 3, 2011 (gmt 0)

[Ff] with [NC] is redundant ;)

Sam234




msg:4347437
 10:30 pm on Aug 3, 2011 (gmt 0)

Thanks, I realised it was a redirect, just did'nt bother stating that.

Thanks for pointing out the NC, missed that.

And thanks for pointing out the .* issue, your suggested replacement works great.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved