How to block referral traffic from multiple referrers and subdomains in .htaccess file?

坚强是说给别人听的谎言 提交于 2019-12-01 19:29:54

Your current code means:

If referer is "sociel-buttons.com"
AND
If referer is "googlsucks.com"
AND
etc...

Which is not what you want (always false: that's why it never happens).
Instead, you have to use OR flag (you want OR boolean conditions in your case)

RewriteCond %{HTTP_REFERER} ^(www\.)?([a-z0-9-]+)\.social-buttons\.com$ [NC,OR]
RewriteCond %{HTTP_REFERER} social-buttons\.com [NC,OR]
RewriteCond %{HTTP_REFERER} googlsucks\.com [NC,OR]
RewriteCond %{HTTP_REFERER} 4webmasters\.org [NC,OR]
RewriteCond %{HTTP_REFERER} aliexpress\.com [NC,OR]
RewriteCond %{HTTP_REFERER} best-seo-solution\.com [NC,OR]
RewriteCond %{HTTP_REFERER} best-seo-offer\.com [NC,OR]
RewriteCond %{HTTP_REFERER} buttons-for-website\.com [NC,OR]
RewriteCond %{HTTP_REFERER} www\.myothertestdomain\.com [NC]
RewriteRule ^ - [F]
Carlos Escalera Alonso

The problem is not only with the code,besides the missing OR , the problem is that you can't stop this spam with the .htaccess file or at least most of them from the .htaccess file.

In your example, the only ones that will be blocked are best-seo and buttons-for-website, these two access your website with crawlers(bots), and are commonly called Crawler Referrer Spam

People get confused and think the .htaccess rule worked because after a sometime they stop seeing the spam, but the truth is that most of the Referrer Spam come for a few days and then disappear.

The rest lines you added won't have any effect. These use a Google analytics vulnerability to show ONLY in your Google Analytics. The .htaccess file "block the access" to your site, but they never visit/access your site. These are commonly called Ghost Referrer Spam.

The only way for now is to make filters in Google Analytics you can either make a filter for each one or make a filter for Valid Hostnames in your GA that way you will don't have to worry about new Ghost Referrer Spam.

You can check this answer for more details https://stackoverflow.com/a/28354319/3197362

You should be using the OR flag for each condition except the last one:

RewriteCond %{HTTP_REFERER} ^(www\.)?([a-z0-9-]+)\.social-buttons\.com$ [NC,OR]
RewriteCond %{HTTP_REFERER} social-buttons\.com [NC,OR]
RewriteCond %{HTTP_REFERER} googlsucks\.com [NC,OR]
RewriteCond %{HTTP_REFERER} 4webmasters\.org [NC,OR]
RewriteCond %{HTTP_REFERER} aliexpress\.com [NC,OR]
RewriteCond %{HTTP_REFERER} best-seo-solution\.com [NC,OR]
RewriteCond %{HTTP_REFERER} best-seo-offer\.com [NC,OR]
RewriteCond %{HTTP_REFERER} buttons-for-website\.com [NC,OR]
RewriteCond %{HTTP_REFERER} www\.myothertestdomain\.com [NC]
RewriteRule .* - [F]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!