5. SetEnvIf Order Allow Deny | .htaccess Tutorial
SetEnvIf with Order Allow, Deny directives
The SetEnvIf directive can be used together with:
- Allow directive
- Deny directive
- Order Allow,Deny
- Order Deny,Allow
SetEnvIf with Order Allow, Deny SYNTAX:
1 2 | SetEnvIf attribute regex varName=varValue |
If the request matches against the attribute
that matches against the regex
, then the env
is set, and you can Allow/Deny based on it.
The attribute
can be:
- an HTTP request header field:
- Accept, Accept-Charset, Accept-Encoding, Accept-Language, Host, Range, Referer, User-Agent...
- a set of request headers specified through a regular expression
- one of the followings:
- Remote_Host, Remote_Addr, Server_Addr, Request_Method, Request_Protocol, Request_URI
- the name of an environment variable previousely set by SetEnvIfNoCase directive
The regex
is a regular expression; must match against the attribute
specified.
The varName
is the name of the environment variable, your choice, with optionally a value.
SetEnvIf with Order Allow, Deny Examples
1. attribute
is an HTTP request header field
1 2 | SetEnvIf User-Agent "*.Trident.*$" noIE |
1 2 | SetEnvIf User-Agent ninja ninjaout |
1 2 | SetEnvIf Host d0main1.com okdom |
1 2 | SetEnvIf Referer www.domainw.com noref |
1 2 | SetEnvIf Accept-Language en-US okUS |
2. attribute
is one of the followings: Remote_Host, Remote_Addr, Server_Addr, Request_Method, Request_Protocol, Request_URI
1 2 | SetEnvIf Remote_Host 178-137-19-143-Ivv.broadband.kyivstar.net botout |
1 2 | SetEnvIf Remote_Addr 178.137.19.143 user178 |
1 2 | SetEnvIf Server_Addr 178.137.19.143 no124referer |
1 2 | SetEnvIf Request_Method POST postforbidden |
1 2 | SetEnvIf Request_Protocol "HTTP/1.1" protone |
1 2 | SetEnvIf Request_URI "index.html" notoindexhtml |
Video demonstration SetEnvIf with Deny directive .htaccess file
SetEnvIf, Deny directives Tutorial
min | video details |
---|---|
00:01 | SetEnvIf, Deny from env, HTTP request header field (UA) modified |
00:09 | the syntax is important |
01:12 | Example: request header field (User-Agent) modified |
02:59 | User-Agent modified |
03:14 | Noteserver access: allowed |
03:38 | Noteserver access: denied |
03:48 | Server access denied to all User-Agents with a 'ninja' value through .htaccess, directives:SetEnvIf User-Agent ninja ninjaoutDeny from env=ninjaout |
04:00 | The request matched against theattribute (User-Agent) that matched against the regex (ninja)→ the env was set (ninjaout) and we were able to Deny based on it |
04:16 | ninja UA removed, Server Access allowed |
For more information about the apache.org
file, please see the spec: