views:

40

answers:

2

For some reason, we recieving a bunch of inbound traffic on URLs that contain HTML entities.

E.g.

http://www.ourdomain.com/index.php?key1=value1&key2=value2

I want to create a RewriteRule to replace these HTML entities (specifically & with &), and forward people on to the corrected address.

I'm tried these:

RewriteRule & & [R=permanent]

and

RewriteRule & &

But nothing is happening.

(BTW. I know this is a horrible and dangerous way to solve someone else's problem, but I just need to get it done. temporarily)

Thanks for any help...

A: 

For any rewrite to work, the rewrite engine must be on, but perhaps you already have that fixed:

# following statement must come before any other rewrite statements
# and must be enabled per virtual host configuration
RewriteEngine On

The ampersand is a special metacharacter. Apparently the people that have copied and pasted your links did so inside an editor which duely escaped the ampersands into & (or, more technically correct, & if it happened inside an a-tag). I'm not 100% sure, but I find it likely that the ampersand should be escaped, try this:

RewriteRule \& \&

To find out how the rewrite goes, what it rewrites and why, you should turn rewrite logging on (make sure Apache can write to the location of RewriteLog):

RewriteLog "somepath.log"
RewriteLogLevel 3

where level 9 is the highest.

Abel
I thin I've discovered why it doesn't work: the RewriteRule isn't applied to the query string part of the URL... I'm trying to find if it's possible to re-write the query string portion, but it's not looking likely...
aidan
+1  A: 

This should do the trick;

RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)&(.*)
RewriteRule .* /index.php?%1&%2 [N,R=301]

This essentially says;

  • Is there an & in the query string?
  • If yes, match everything before and after it, then sandwich them together with an &
  • The N flag in the rule effectively says 'keep doing this until the condition no longer makes a match'

Check out the mod_rewrite documentation, including RewriteCond and rule flags.

TheDeadMedic
that's brilliant, thanks :)
aidan