htaccess use of Order Deny,Allow - apache

I'm using Order Deny,Allow in my .htaccess file, without success. IP's I need to prevent access to are going right through with no troubles. I read that as of Apache 2.4 Order Deny,Allow would no longer work; first is that factual and second if so what has replaced it? I cannot access my httpd.conf file, so Require and Require not is not an option for me.
I only have access to .htaccess, how can I accomplish banning by ip or if no longer possible, redirecting by ip. Below is a snipit of my Order Deny,Allow if it matters. Thanks in advance!
Order Deny,Allow
Deny from 123.125.71.*
Deny from 123.125.71.121.some.domain.com
Deny from some.domain.com
Allow from All
(as you can see, I'm attempting to block the same ip, in various formats but no matter the format, the traffic continues to go through.)

To push the undesired ip to 403 ,you can use the following rule :
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^00\.00\.00\.00$
RewriteRule ^ - [R=403,L]
Replace 00.00.00.00 with your undesired ip address.
To ban multiple ipaddress, you can add multiple conditions seprated by a [OR] flag to your rule :
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^00\.00\.00\.00$ [OR]
RewriteCond %{REMOTE_ADDR} ^00\.00\.00\.00$
RewriteRule ^ - [R=403,L]

Related

.htaccess file to block all IPs except 4 and always allow requests to /api/ for averyone

I am trying to disallow all requests by
RewriteCond %{HTTP:X-FORWARDED-FOR} !=67.x.x.x
RewriteCond %{REQUEST_URI} !^api
RewriteRule ^(.*)$ - [R=403,L]
ErrorDocument 403 "<html><hea....
So the IP 67 should be allowed for all REQ - but the directory /api/ should be accessible for everyone.
How can I do that?
You Can apply some of these directives.
Also, am thinking that your api is a directory
order deny,allow
deny from all
allow from 222.333.444, 67.8.9.9 # ALLOWED IPS MUST BE SEPARATED BY COMMAS
<Directory /api>
# All access controls and authentication are disabled
# in this directory
Satisfy Any
Allow from all
</Directory>
To deny access to all but 4 specific ip addresses you can use a negative RewriteCond and regex pattern to match against the allowed ip addresses something like the following :
RewriteCond %{REMOTE_ADDR} !(ip1|ip2|ip3|ip4)
If you do not want your /api uri to be redirected to 403 you can exclude it in your Rewrite pattern so that the uri is available for both allowed and denied ip addresses.
Full code :
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !(ip1|ip2|ip3|ip4)
RewriteRule !api - [R=403,L]

Block specific url for specific country via .htaccess

I'm looking to achieve blocking specific urls for 1 specific country via .htaccess. What I got so far is a block to the entire site, but what I'm looking is to block a specific url of the site. Here is what I got.
<Limit GET POST>
order allow,deny
deny from 2.20.179.0/24
deny from 2.20.185.0/24
deny from 2.22.230.0/24
deny from 2.136.0.0/15
deny from 2.138.0.0/15
allow from all
</Limit>
Having that, how could I block the url http://domain.com/en/profiles/kasper, for that specific IP ranges / country? What is the right way to include that url in the htaccess?
Thank you in advance
You can use mod_rewrite if you are in htaccess and want to block URLs for certain IPs:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^2\.20\.(179|185)\.(\d|1\d|2[0-4])$ [OR]
RewriteCond %{REMOTE_ADDR} ^2\.22\.230\.(\d|1\d|2[0-4])$ [OR]
RewriteCond %{REMOTE_ADDR} ^2\.(136|138)\.0\.0$
RewriteRule ^en/profiles/kasper(/.*)$ - [F,NC]

htaccess redirect but exclude whole ipv6 prefix

I'm trying to redirect visitors from my old domain to a new domain using .htaccess, however I would like to be able to visit the old domain myself by excluding my ipv4 & ipv6 from the redirect. If I fill in my exact ipv6 it's working but as it's changing often I would like to exclude my whole ipv6 prefix from the redirect.
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^12.345.678.901
RewriteCond %{REMOTE_ADDR} !^1234:123:1234:1:1234:1234:1234:1234
RewriteCond %{REMOTE_ADDR} !^1234:123:1234::/48
RewriteRule .* http://www.example.com [NC,R=301,L]
The first two RewriteConds are working, the third unfortunately is not :(
Note: replaced my ip with some fake numbers
Thanks!
Fixed it by using a different approach
<Files *>
ErrorDocument 403 http://www.example.com
Order deny,allow
Deny from all
Allow from 12.345.678.901
Allow from 1234:123:1234::/48
</Files>
However I would still like to know why the first option is not working

How can I limit access to a virtual host (subdomain) to a few specific IPs?

The catch is that this virtual host points to the same root location as the main site. It is meant to just mirror the content of www.mysite.com on sub.mysite.com. This prohibits me from using allow and deny rules in my .htaccess, since that would affect www.mysite.com traffic as well. I've also tried using:
RewriteCond %{HTTP_HOST} ^sub.mysite.com$
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$ [OR]
RewriteCond %{REMOTE_ADDR} !^123\.55\.76\.234$ [OR]
RewriteCond %{REMOTE_ADDR} !^12\.54\.67\.87$
RewriteRule ^.*$ http://www.mysite.com [R=301]
The IP addresses don't fall into a range, so I think generating a single-line REGEX to cover all of them wouldn't work, and using a series of conditionals like the above simply didn't work, and seemed very messy. The above IPs are just examples. I would need to limit access to roughly 10 unique IPs. Any ideas?
Try to define allow and deny in the virtualhost under the directory node. That should only take effect on the virtualhost you want to.
<VirtualHost 111.22.33.55:8080>
ServerName sub.mysite.com
<Directory proxy:>
Order Deny,Allow
Deny from all
Allow from 111.22.33
</Directory>
</VirtualHost>

Redirect all IPs except those whitelisted

I want to protect some subdomains from the public. Restriction should be done against a whitelist of IPs.
Infinite loop due to the redirect is not a problem as its not the www-domain.
I tried this http://discussions.apple.com/message.jspa?messageID=2411725, but couldnt get it to work.
However I did try this first
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$ [OR]
RewriteCond %{REMOTE_ADDR} !^213\.45\.67\.89$
RewriteRule ^/.* http://www.mydomain.com [R]
.. but didnt work.
What am I doing wrong ?
This kind of thing is actually exactly what Apache's Allow and Deny directives are intended for. Inside the <VirtualHost> block for the domain you want to restrict access to, put this:
<Location />
Order allow,deny
Allow from all
Deny from 123.45.67.89
Deny from 213.45.67.89
</Location>
However, this would produce a 403 (forbidden) error, which doesn't redirect to your www domain by default. I think you can make it do so by adding the directive
ErrorDocument 403 http://www.example.com
You have to combine the RewriteCond directives with AND instead of OR as you want to redirect if both conditions are true (therefor the IP address is neither X nor Y). So try this:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$
RewriteCond %{REMOTE_ADDR} !^213\.45\.67\.89$
RewriteRule ^ http://www.example.com/ [R]