Htaccess Access to File by Ip Range

htaccess access to file by ip range

Just add a FilesMatch or Files directive to limit it to a specific script.

The following would block acces to all scripts ending in "admin.php" :

<FilesMatch "admin\.php$">
Order deny,allow
Deny from all
Allow from 10.0.0.0/24
</FilesMatch>

The following would ONLY block admin.php :

<Files "admin.php">
Order deny,allow
Deny from all
Allow from 10.0.0.0/24
</Files>

For more information refer to the apache docs on Configuration Sections.

I want to allow an IP range in .htaccess

A̶l̶l̶o̶w̶ ̶f̶r̶o̶m̶ ̶1̶4̶5̶.̶0̶5̶0̶.̶0̶3̶9̶.̶0̶0̶8̶ ̶1̶4̶5̶.̶0̶5̶0̶.̶0̶3̶9̶.̶0̶1̶7̶

C̶h̶e̶c̶k̶ ̶h̶e̶r̶e̶ ̶f̶o̶r̶ ̶d̶e̶t̶a̶i̶l̶s̶.̶


(late) EDIT:

My bad, my previous answer never worked, at this time I answered without really understanding the problem.

Option #1: define all IP

Here's the only way to do it:

Allow from 145.50.39.8
Allow from 145.50.39.9
Allow from 145.50.39.10
Allow from 145.50.39.11
Allow from 145.50.39.12
Allow from 145.50.39.13
Allow from 145.50.39.14
Allow from 145.50.39.15
Allow from 145.50.39.16
Allow from 145.50.39.17

Option #2: use a partial IP

If you want something more generic, you can use a partial IP:

Allow from 145.50.39

But this will allow IP from 145.50.39.0 to 145.50.39.255.

Option #3: use a netmask or a CIDR

To get even closer, you can use a netmask or a CIDR:

Allow from 145.50.39.0/255.255.255.224

or

Allow from 145.50.39.0/27

This will allow IP from 145.50.39.0 to 145.50.39.31.

Adding a range of allowed IP addresses to htaccess

Yes, there is !

If you want to block a range of IPs then you can leave off the last octet of the IP address, for example:

RewriteCond %{REMOTE_ADDR} !^123\.123\.123

and you can shorten that even further if needed:

RewriteCond %{REMOTE_ADDR} !^123\.123

So for example, using the first option, you would be blocking the IP range of 123.123.123.1 - 123.123.123.255. So it covers the entire range.

This can also be achieved using the deny from rule in .htaccess.

How do I allow these two IP ranges in my htaccess file?

The possible solution you posted will allow the ranges 195.35.90.1 - 195.35.90.255 and 195.35.91.1 - 195.35.91.255 in addition to the 11.x.x.x IPs you allowed.

You can check out here for more details on the syntax of the Allow directive.

Deny all, allow only one IP through htaccess

order deny,allow
deny from all
allow from <your ip>

.htaccess: how to restrict access to a single file by IP?

This will allow either someone from IP 127.0.0.1 or logged as a valid user. Stick it either in your config or .htaccess file.

    <Files learn.php>
Satisfy any
Order deny,allow
Deny from all
Allow from 127.0.0.1

AuthType Basic
AuthName "private"
AuthUserFile /var/www/phpexperts.pro/.htpasswd
AuthGroupFile /dev/null
Require valid-user
</Files>

IP Alone:

    <Files learn.php>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Files>

That definitely answers your question.

.htaccess limit IP range

According to your comment above you request with a local address, not with the one you expected. So you have to grant access to that address to:

# Limite access to 160.98.xxx.xxx
<Limit GET POST>
order deny,allow
deny from all
allow from 160.98.
allow from 127.0.
</Limit>

Most likely that is not your ultimate goal, but it answers the question. If you want to test if the specified address range starting with 160.98. has access, then you have to make a request from such an address. Currently you don't, since obviously you test from the same system which will always use the loopback address.

Allow directory access to certain IP addresses

You can do this simply with the following code.

order deny,allow
deny from all
allow from x.x.x.x
allow from x.x.x.x
allow from x.x.x.x

Using the above lines, it's important that you keep the order the same. Denying from all before allowing from specific addresses will allow access to only a few IPs while allowing before denying will deny access to all.

For more information, try searching google for htaccess deny/allow commands.

htaccess: how to restrict IP Address for URL path?

Using mod_rewrite to control access
Here is the documentation of it, you may go to section "Blocking of Robots" to block IP range which trying to access particular url.

I think you may try this, I am not sure whether is works, but still try it, hope it works for you.

RewriteEngine on # this is for opening the module
RewriteCond %{REMOTE_ADDR} =123\.45\.67\.[8-9]
RewriteRule ^/locations/europe/contactus/ - [F]


Related Topics



Leave a reply



Submit