How to set rewrite cond apache by Ip range - .htaccess

I have this rewrite cond in .htaccess
RewriteCond %{REMOTE_ADDR} !^195.16.40.50
How to set RewriteCond by ip range from 192.168.0.0 to 192.168.32.255

This condition should match your ip range:
RewriteCond %{REMOTE_ADDR} ^195\.168\.([0-9]|[1-2][0-9]|3[0-2])\.[0-9]+$

Related

RewriteCond %{REMOTE_ADDR} [IPv4] prevent catch IP range?

A RewriteCond %{REMOTE_ADDR} with a IPv4 catch a IP range. Example:
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.89
# catch 123.45.67.89
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.8
# catch 123.45.67.8[0-9]
# or as omit
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89
...
Eventually catch it more. I have no further tested. My actuall IP is (similar to the example) "123.45.67.89".
The RewriteCond is for a RewriteRule to rewrite temporary to a maintenance.php. I insert my IP. Only my IP don't rewrite to maintenance.php.
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89
RewriteRule . maintenance.php [QSA,NC,L]
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.89
RewriteRule . index.php [QSA,NC,L]
With use this with IP 123.45.67.89, i becomes the index.php. With use this with IP 123.45.67.8, i becomes the index.php. With use this with IP 123.45.67.890, i becomes the maintenance.php. How can i prevent to catch / omit more than my IP adress? Besides, i have dynamic IP adress. It's possible my IP is 123.45.67.8.
EDIT: thanks to #anubhava.
Solution: ^123\.45\.67\.89$
My actuall IP is (similar to) 123.45.67.89. How can i prevent to catch / omit more than my IP address? Besides, i have dynamic IP address. It's possible my IP is 123.45.67.8
if you want to match only single IP then you will have to hardcode that IP by using
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$
Note that you need to escape dots in your IP address to avoid it matching any character.
Your rules can be like this:
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$
RewriteRule . maintenance.php [L]
RewriteRule . index.php [L]

Redirect based on a URL rather than an IP address in .htaccess

I use a redirect in .htaccess to redirect any user asking for domain.com/admin to domain.com as follows:
RewriteCond %{REMOTE_ADDR} !^101.101.101.101
RewriteCond %{REQUEST_URI} admin [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/ [R=302,L]
This redirects all visitors other than those coming from the fictional IP address 101.101.101.101 and it works fine.
I want to be able to replace the IP address with a URL like abc.domain.com as the IP address 101.101.101.101 can vary over time and a dynamic DNS has been set up at abc.domain.com to track the updated IP address. Is this possible?
I have tried putting the URL in single quotes, double quotes, etc. to no avail.
Have you tried something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.from_domain\.com [NC]
RewriteRule ^(.*)$ http://www.to_domain.com/$1 [R=302,L]
Replace this line :
RewriteCond %{REMOTE_ADDR} !^101.101.101.101
With this :
RewriteCond %{HTTP_HOST} !^(www\.)?abc\.domain\.com$
Clear browser cache then test , if it is OK , replace R=302 to R=301 to be permenant redirection.

Rewrite rule to show maintenance page for IPs not in the list

I need to allow site access to only development team accessing from IPs 111.111.111.111 and 222.222.222.222 and for the rest of the visitors would like to show temporary maintenance page brb.html
I tried this with the following condition and rewrite rule and it got into a redirect loop. Any insights on how to make this work?
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^222\.222\.222\.222
RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111
RewriteCond %{SCRIPT_FILENAME} !^brb.html
RewriteRule ^.*$ /brb.html [R=307,L]
Note: IPs used above are not the real ones.
You can use:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^(222\.222\.222\.222|111\.111\.111\.111)
RewriteRule !^brb\.html$ /brb.html [R=307,L]
And test in a new browser.
!^brb\.html$ in rewrite rule pattern will stop looping for you.
SOLUTION:
Because of CloudFlare I had to use RewriteCond %{HTTP:X-FORWARDED-FOR} instead of RewriteCond %{REMOTE_ADDR}

DDOS mod Rewrite IP Request

We're receiving a DDOS attack from a specific range of IPs (192.168.0-255.0-255). In our htaccess file we've attempted to forward their requests to a static HTML file but only half the requests are being blocked. Does anyone see why that would be?
RewriteCond %{REMOTE_ADDR} ^(10\.0\.0\.1|192\.168\.[0-9]{0,3}\.[0-9]{0,3})$
RewriteCond %{REQUEST_URI} [^/etc/blocked_ip.html]
RewriteRule ^(.*)$ /etc/blocked_ip.html [R=301,L]
and our access logs show:
2014-06-27 11:59:03 192.168.20.232 - 1.2.3.4 443 GET /etc/blocked_ip.html ?
2014-06-27 11:59:08 192.168.20.231 - 1.2.3.4 443 GET /video/832
Note: I've substituted the actual IP ranges with private ranges.
Thanks for any suggestions.
Actually your rewrite condition is incorrect:
RewriteCond %{REQUEST_URI} [^/etc/blocked_ip.html]
Probably you meant:
RewriteCond %{REQUEST_URI} !^/etc/blocked_ip\.html
You rule can be shortened to:
RewriteCond %{REMOTE_ADDR} ^(10\.0\.0\.1|192\.168\.[0-9]{1,3}\.[0-9]{1,3})$
RewriteRule !^etc/blocked_ip\.html$ /etc/blocked_ip.html [R=301,L,NC]
Also make sure this is your very first rule in your .htaccess.

htaccess rules filtered on IP

I have the following htaccess rule I'd like to apply for every IP address apart from mine. I basically want to show a "site is down" page for everyone apart from myself. How is this best achieved?
RewriteEngine on
RewriteCond %{REQUEST_URI} !/indexTEMP.php$
RewriteRule $ /indexTEMP.php [R=307,L]
The Apache variable is REMOTE_ADDR.
Untested but should work:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/indexTEMP.php$
RewriteCond %{REMOTE_ADDR} !^192\.168\.0\.1$
RewriteRule $ /indexTEMP.php [R=307,L]
this applies the rule to every IP except 192.168.0.1.

Resources