SetEnvIfNoCase redirection

…衆ロ難τιáo~ 提交于 2019-12-13 02:49:55

问题


Hi all ive been learning wildcard methods using SetEnvIfNoCase User-Agent and wildcards,
But using the example below.only serves a 403 error page if a useragent matches the wildcards.
but what i want is to redirect the "user-agent" to another website such as a black hole or spam page.
using something like RewriteRule ^(.*)$ http://send junk to here/

SetEnvIfNoCase User-agent "(B2|Bac|Bad|Bag|Bai|Bast|Batch|Bing|Bite|Bla|Blex)" bad_bot=yes
#
Order Allow,Deny
Allow from All
Deny from env=bad_bot

what can i replace the Deny from env=bad_bot with to make it redirect to the wanted website instead of serving the 403 error page.


回答1:


Have your rewrite rule like this in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond  %{HTTP_USER_AGENT} B2|Bac|Bad|Bag|Bai|Bast|Batch|Bing|Bite|Bla|Blex [NC]
RewriteRule  !^spam/ http://officeofstrategicinfluence.com/spam/ [L,NC,R=302]

UPDATE:: In response this comment by OP

1- adding a new line of filters do i need to change the [NC] ? and 2- if i wanted to add a single word by itself do i still use RewriteCond %{HTTP_USER_AGENT} ^word [NC]? with the ^

Try this code:

RewriteCond  %{HTTP_USER_AGENT} B2|Bac|Bad|Bag|Bai|Bast|Batch|Bing|Bite|Bla|Blex [NC,OR]
RewriteCond  %{HTTP_USER_AGENT} foo|bar|etc [NC]
RewriteRule  !^spam/ http://officeofstrategicinfluence.com/spam/ [L,NC,R=302]



回答2:


The RewriteCond directive can filter by server variables, including environment variables like your bad_bot. Syntax is:

%{ENV:variable}, where variable can be any environment variable, is also available. This is looked-up via internal Apache httpd structures and (if not found there) via getenv() from the Apache httpd server process.

But it can also filter by HTTP headers as well so you don't need your env variable:

RewriteCond  %{HTTP_USER_AGENT}  ^Mozilla
RewriteRule  ^/$                 /homepage.max.html  [L]

RewriteCond  %{HTTP_USER_AGENT}  ^Lynx
RewriteRule  ^/$                 /homepage.min.html  [L]

RewriteRule  ^/$                 /homepage.std.html  [L]


来源:https://stackoverflow.com/questions/19786398/setenvifnocase-redirection

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