mod_rewrite RewriteRule to handle HTML Entities

前端 未结 2 804
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 16:12

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&am

相关标签:
2条回答
  • 2021-01-03 16:55

    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.

    0 讨论(0)
  • 2021-01-03 16:55

    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.

    0 讨论(0)
提交回复
热议问题