Block 'kw=' Query String

早过忘川 提交于 2019-12-10 11:55:58

问题


I keep getting nonsense hits to my site like:

  • myurl.com/?kw=antivirus*protection
  • myurl.com/?kw=a+casino
  • myurl.com/?kw=.confused.com
  • myurl.com/?kw=whatevernonsense

Also, if it matters, it is a self-hosted Wordpress site. I've tried many permutations to the following, but it isn't working:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{QUERY_STRING} kw
    RewriteRule ^(.*)$ - [F,L]
</IfModule>

Edit: I've tested the above code in a separate directory, and it works fine. Below is the Wordpress chunk I've narrowed it down to:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{QUERY_STRING} kw
    RewriteRule ^(.*)$ - [F,L]
</IfModule>

# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
# END WordPress

回答1:


I was able to fix this by adding the following to the WP chunk of the .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On

    # These 2 lines block any request coming in with `/?kw=`
    RewriteCond %{THE_REQUEST} \s/+[^?]*\?kw=
    RewriteRule ^ - [F,L]

    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
# END WordPress


来源:https://stackoverflow.com/questions/28969856/block-kw-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!