Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a web server behind a load-balancer.

I need to add a conditional redirect to my .htaccess in order to display a maintenance page whenever we take the site offline for maintenance. This part is straightforward:

RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.php [R=302,L]

However I want to add in a condition that if the visitor's IP address is my own, it will not redirect me to the maintenance page and that I would be able to see and test the site as if it was online. This part is normally also straightforward:

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.php [R=302,L]

However, because my web server is behind a load balancer, REMOTE_ADDR is resolved to the internal IP address of the Load Balance server.

How can I alter this to look for the forwarded IP address? I know in PHP you can use $_SERVER['HTTP_X_FORWARDED_FOR'] to get the forwarded IP address. I've tried a few things in the .htaccess but no luck:

%{X_FORWARDED_FOR}
%{HTTP:X_FORWARDED_FOR}
%{HTTP_X_FORWARDED_FOR}

SOLUTION

I got the following to work:

%{HTTP:X-FORWARDED-FOR}
share|improve this question
    
%{HTTP:X-Forwarded-For} is the correct one. Is it case sensitive? –  Jeff Strunk Nov 10 '11 at 19:20
    
All Caps worked for me. I guess it's not case sensitive or else it's an alias or something. –  Jakobud Nov 11 '11 at 15:49
add comment

4 Answers 4

You need mod_rpaf. This module will rewrite REMOTE_ADDR in apache with another header, such as x-forwarded-for. Very useful for making PHP apps behave with load balancers.

share|improve this answer
add comment

Use %{HTTP:X-FORWARDED-FOR} instead of %{REMOTE_ADDR}

share|improve this answer
    
I know you already answered it in your question, but you didn't have an answer below that matched. Thank you for your answer! Saved me today. –  Ryan Oct 9 '12 at 13:51
add comment

If you have two environments, say one production behind the load balance and a development or staging not behind the load balance, and you want to use the same .htaccess file, you will need both %{HTTP:X-FORWARDED-FOR} and %{REMOTE_ADDR} -- assign IP addresses to both conditions.

share|improve this answer
1  
Also, on some apache servers, the backslash will confuse the server. Simply put !^123.123.123.123 (without the backslashes, to mean if not IP 123.123.123.123) if you cannot get an iteration with backslashes to work. –  Andrew S May 8 '13 at 2:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.