mod_rewrite RewriteRule to handle HTML Entities
For some reason, we recieving a bunch of inboun开发者_JS百科d 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...
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.
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.
精彩评论