Home > other >  How to send a header in apache based on remote address?
How to send a header in apache based on remote address?

Time:02-22

Using Apache 2.4's htaccess file, some redirects and headers are configured.

How can I configure it to send a specific header to all but specific IP?

Something like this wrong code:

RewriteCond %{REMOTE_ADDR} ^1.2.3.4$
Header always set BLAH "is blah"

RewriteCond %{REMOTE_ADDR} !^1.2.3.4$
Header always set BLAH "is no so blah!"

CodePudding user response:

In Apache 2.4 there is this expression option detailed in here

Basically do

<IfModule mod_headers.c>
    <If "%{REMOTE_ADDR} == '1.2.3.4'">
        Header always set BLAH "is blah"
    </If>
    <Else>
        Header always set BLAH "is NOT blah"
    </Else>
</IfModule>

CodePudding user response:

You can set an environment variable if the request originates from a specific IP address(es) and set the header conditionally when that variable is or is not set using the env= argument to the Header directive.

For example, using mod_setenvif and mod_headers:

SetEnvIf Remote_Addr "^1\.2\.3\.4$" SPECIAL_IP_ADDRESS
Header always set BLAH "is blah" env=SPECIAL_IP_ADDRESS
Header always set BLAH "is NOT blah" env=!SPECIAL_IP_ADDRESS

Works with both Apache 2.2 and 2.4

  • Related