Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .htaccess to restrict visits to login page from all IP's except mine

I have a simple PHP website with a login page url like http://sample.com/login

I need to restrict access to the login page from any IP addresses other than mine. But, I dont want to restrict access to any of the other pages on the site. I added the following code to the top of my .htaccess file, but it doesn't seem to be working correctly. Any help anyone can give me with the correct code to add to my .htaccess to only block access to /login/ from other IP's would be much appreciated!

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^xxx.xxx.xxx.xxx
RewriteRule ^/login/$ http://sample.com/$1 [L,R=301,NC]
like image 636
user3826864 Avatar asked Dec 20 '25 16:12

user3826864


1 Answers

You could use <Files ...>, which would block that specific file from access from everywhere but a given IP, using deny and allow. This is a relative path, and this .htaccess should be placed in the same folder as your login-file.

<Files login.php>
    Order deny,allow
    Deny from all
    Allow from xxx.xxx.xxx.xxx
</Files>

Replace xxx.xxx.xxx.xxx with your IP. Remember that if you change IP-address (if its not static for example), this will not be a viable solution.

like image 97
Qirel Avatar answered Dec 22 '25 13:12

Qirel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!