Allow access to all files from only 1 IP address and redirect all others to other file

19,283

Solution 1

You can use mod_rewrite to do that. Add the following in your .htaccess file:

Code:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule index.php$ /subscribe.php [R=301,L]

Alternative solution:

<?php $allow = array("123.456.789", "456.789.123", "789.123.456"); //allowed IPs
if(!in_array($_SERVER['REMOTE_ADDR'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow)) {
    header("Location: http://domain.tld/subscribe.php"); //redirect
    exit();
} ?>

Hope this helps!

Solution 2

You can use mod_rewrite for the same.

Add following in your .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^80\.40\.20\.[0-9]$ # your ip here
RewriteCond %{REQUEST_URI} !^/subscribe.php
RewriteRule .? /subscribe.php [R,L]
</IfModule>
Share:
19,283

Related videos on Youtube

Nart Barileva
Author by

Nart Barileva

Updated on September 15, 2022

Comments

  • Nart Barileva
    Nart Barileva about 1 month

    I'm not sure if this has been answered before but I tried looking for it. Anyways, I'm currently developing a website but I would like to make the actual site content only accessible from my IP address. I then want .htaccess to redirect all other IP address to a separate file on my server. That one file would be called subscribe.php.

    I've tried a couple things but nothing provided me with the result I wanted. I know my server allows .htaccess to be used since I've used it to change some other things such as preventing caches.