Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess - how to allow multiple ips to access the site

Tags:

.htaccess

I'm using this code to allow only my ip to access the site

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx
 RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintenance.html [R=302,L]
</IfModule>

Now I would like to allow my friends ip to access the site. How to specify multiple ips in htaccess?

like image 822
Giri Avatar asked Mar 21 '12 13:03

Giri


2 Answers

Use an .htaccess file to restrict the site and add:

#...
Allow from IP/SubnetMask
Allow from IP/SubnetMask
#...

See documentation for more details.

like image 99
Stefan Avatar answered Dec 14 '22 07:12

Stefan


The trick is to add a rule that does nothing for the allowed IPs, than to add the rule for the maintenance; as you can mark the rule as Last, which means next rules won't be processed (unless the rule didn't modify the address, which will trigger one more pass trough the .htaccess). In your case it will look like:

<IfModule mod_rewrite.c>
 RewriteEngine on

 RewriteCond %{REMOTE_ADDR} xxx\.xxx\.xxx\.xxx [OR]
 RewriteCond %{REMOTE_ADDR} xxx\.xxx\.xxx\.xxy [OR]
 RewriteCond %{REMOTE_ADDR} xxx\.xxx\.xxx\.xxz
 RewriteRule .* - [L] #do notthing

 #if we are here, the IP is not in the allowed list, redirect
 RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintenance.html [R=302,L]
</IfModule>
like image 28
Maxim Krizhanovsky Avatar answered Dec 14 '22 05:12

Maxim Krizhanovsky