简体   繁体   中英

How to specify where to get IP from in htaccess

Okay so we use some simple IP authentication with htaccess for a folder on our site:

order deny,allow
deny from all
allow from 1.1.1.1

But with the new CDN we are on, the actual users IP address comes through in a different server variable, lets say:

$_SERVER['absolutely_true_ip'];

whereas

$_SERVER['remote_addr'];

is not the users true IP

Is there anyway to tell htaccess where to look for the IP we want to authenticate with?

Yes this is possible.

X-FORWARDED-FOR is used by most servers (CDNs, Proxys) to send the originating ip (the users ip). So this should to the job:

SetEnvIf X-FORWARDED-FOR 1.1.1.1 allow
order deny,allow
deny from all
allow from env=allow

If your CDN has a different variable name, just edit the first line.


EDIT:

If you want to allow multiple ip's, just duplicate the first line:

SetEnvIf X-FORWARDED-FOR 1.1.1.1 allow
SetEnvIf X-FORWARDED-FOR 2.2.2.2 allow
SetEnvIf X-FORWARDED-FOR 3.3.3.3 allow
order deny,allow
deny from all
allow from env=allow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM