skip to Main Content

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!"

2

Answers


  1. Chosen as BEST ANSWER

    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>
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search