Restricting access using the X-Forwarded-For header in Apache

.htaccessapache-2.2

I'm trying to set up access for a directory using .htaccess rules. Apparently, the remote IP is always localhost due to the way the provider ( SourceForge.net ) has set things up.

I've noticed that the correct IP is set in the X-Forwarded-For header.

How can I allow access only to some known hosts based on the X-Forwarded-For header?

Best Answer

RewriteEngine On
SetEnvIfNoCase X-Forwarded-For .+ forwarded=yes
RewriteCond %{ENV:forwarded} yes
RewriteRule ... my conditions ...

X-Forwarded-For can contain more then one ip address if it was forwarded my multiple servers - if that is the case then you need some script to loop trough this list and processes it.