Setting a 302 redirect using htaccess with CloudFlare?
Trying to limit site access to a single IP while still running the reverse proxy CloudFlare (while testing). Using the standard
Rewrit开发者_运维百科eEngine On
RewriteCond %{REMOTE_HOST} !32\.231\.45\.342
RewriteRule $ http://www.google.com [R=302,L]
This not commented # with cloudflare turned on will always redirect to google, even from the remote host IP.
If I add all of CloudFlare's IPs, that obviously won't help.
The only workaround I see would be to store any IP I want to give access to in an array and check for it via PHP, redirecting elsewhere if not found. However, if I can get away with it I'd rather use htaccess. Thoughts?
mod_cloudflare (https://github.com/cloudflare/CloudFlare-Tools) should fix the IP to being the correct one.
If it doesn't need to be secure then you could use the X-Forwarded-For header like this:
RewriteEngine On
# if X-Forwarded-For is set
RewriteCond %{HTTP:X-Forwarded-For} .
RewriteCond %{HTTP:X-Forwarded-For} !32\.231\.45\.342 [OR]
# if X-Forwarded-For isn't set default to remote_host
RewriteCond %{HTTP:X-Forwarded-For} ^$
RewriteCond %{REMOTE_HOST} !32\.231\.45\.342
RewriteRule $ http://www.google.com [R=302,L]
People might be able to spoof that if they knew the IP was 32.231.45.342 by setting a custom X-Forwarded-For. Would only work if it didn't go through cloudflare though which should change the X-Forwarded-For header to it's own.
精彩评论