0

My application runs on an ubuntu server which sits behind cloudflare. I want to redirect requests for example.com/admin to show 404 if the visitor is not coming from my IP 123.1.2.3

I tried this I also tried using

RewriteCond %{HTTP:CF-CONNECTING-IP} !^(123\.1\.2\.3)$
RewriteRule ^admin.php  - [R=404,L]

But none of them worked.
I have installed mod_cloudflare

Jez D
  • 1,391
  • 1
  • 23
  • 48

4 Answers4

0

Try using this instead:

RewriteCond %{REMOTE_ADDR} !=123.1.2.3
RewriteRule admin.php$ /404.php [R=302,L]

I've set it as R=302 as that is a temporary redirect, I'm assuming you don't want this permanently, but if you do, change it to R=301.

Make sure you clear your cache before testing this.

Joe
  • 4,503
  • 4
  • 28
  • 47
  • Nope that didn't work. Probably because I am using Cloudflare (proxy), which is why I tried x-forwarded-for, as per the link in the question – Jez D Jul 11 '18 at 20:58
0

Try: RewriteCond %{CF-CONNECTING-IP} !123.1.2.3

Jules
  • 1,841
  • 15
  • 18
0

If you are using mod_cloudflare, is it working? Does the PHP below display your actual IP address?

echo $_SERVER['REMOTE_ADDR'];

mod_cloudflare instructions Assuming mod_cloudflare is working correctly, if you had an admin directory you should be able to do something like this.

.htaccess inside admin directory...

order deny,allow
deny from all
allow from 123.1.2.3

Or, if it's just a file that you need to protect, assuming you have mod_rewrite enabled, the following should work (from this question).

.htaccess in the root directory of your website...

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.1.2.3
RewriteRule admin.php$ /404.php [R=301,L]

To enable mod_rewrite in ubuntu...

sudo a2enmod rewrite
sdexp
  • 704
  • 2
  • 13
  • 27
0

Try this one:

RewriteEngine On
RewriteCond %{HTTP:CF-CONNECTING-IP} !=123.1.2.3
RewriteRule (.*) /404 [R]
Mykola
  • 81
  • 1
  • 3