D
D
Danila2016-08-06 17:47:38
htaccess
Danila, 2016-08-06 17:47:38

How to block User-Agent requests via .htaccess?

Hello! Placed in .htaccess rules blocking requests from certain User-Agents. The multi-line construction (bottom) works, but the abbreviated one containing regular expressions does not work, i.e. causes a 404 error for any User-Agent. What could be the reason?

#Эта конструкция не работает - вызывает ошибку 404, когда заходишь на сайт для всех User-Agent.
RewriteCond %{HTTP_USER_AGENT} [SolomonoBot|HaosouSpider|MJ12Bot|AhrefsBot|SemrushBot|360Spider]
RewriteRule ^.*$ - [R=404,L]

#А эта конструкция работает, но она слишком большая. Что я делаю не так?
RewriteCond %{HTTP_USER_AGENT} HaosouSpider [NC,OR]
RewriteCond %{HTTP_USER_AGENT} MJ12bot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} AhrefsBot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} SemrushBot
RewriteCond %{HTTP_USER_AGENT} 360Spider [NC,OR]
RewriteRule .* - [R=404,L]

#Вот так всё работает
RewriteCond %{HTTP_USER_AGENT} (SolomonoBot|HaosouSpider|MJ12Bot|AhrefsBot|SemrushBot|360Spider)
RewriteRule ^.*$ - [R=404,L]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2016-08-06
@redcap152

Try parentheses instead of square brackets.

N
Neocaridina, 2016-08-06
@Neocaridina

SetEnvIfNoCase User-Agent "ahrefs.com" bad_bot
SetEnvIfNoCase User-Agent "megaindex.ru" bad_bot
SetEnvIfNoCase User-Agent "Java/1." bad_bot
SetEnvIfNoCase User-Agent "linkpad.ru" bad_bot
<Limit GET POST HEAD>
Order Allow,Deny
Allow from all
Deny from env=bad_bot
</Limit>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question