htaccess redirect but exclude whole ipv6 prefix - apache

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

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]

htaccess use of Order Deny,Allow

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]

Multiple domains in one htaccess and have limited access to only one

I don't know if it is possible. I have three domains in one .htaccess and pointing to a shared location:
domain-a.com
domain-b.com
domain-c.com
And now i want to block domain-c.com for all traffic and allow access from only two ip's. Domain-a.com and domain-b.com have to stay untouched.
I have tried to use this code in the .htaccess.
order deny,allow
deny from all
allow from x.x.x.x
I just don't know how to get it to work for one domain only instead of three.
To deny access to the domain , you can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain_c.com$
#--allowed ip(s)--#
RewriteCond %{REMOTE_ADDR} !^(ip1|ip2)$
RewriteRule ^ - [F,L]

Rewriting specific host to directories on localhost

I'm currently trying to redirect my local configured domains to its directories.. What is the best way to achieve this using htaccess?
Currently 'localhost' redirects to /Library/WebServer/Documents/ (mac OSX), i want to create an htaccess file in this directory that will lead all domains to its directory.
Example htaccess code of what I'm looking for:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com/$
RewriteRule ^(.*)$ /Library/WebServer/Documents/example.com/$1 [L]
I hope someone can help me out, think there's an easy fix for this!
Kind regards,
This is not possible with .htaccess alone. You need access to the main server configuration file or at least modify the appropriate virtual host section.
If you can modify the main config file, you can add any number of virtual hosts.
If you're allowed to adjust your virtual host only, you must add as many ServerAlias entries to your virtual host as you have domains.
Then you can add the rewrite rules for the various domains
RewriteEngine On
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/example.com/$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/domain.com/$0 [L]
Try this
# get the server name from the Host: header
UseCanonicalName Off
# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /var/www/vhosts/%0/httpdocs
<Location /var/www/vhosts>
AllowOverride All
Options +FollowSymLinks
</Location>
Then, you could try these examples:
example.com would show /var/www/vhosts/example.com/httpdocs
example2.com would show /var/www/vhosts/example2.com/httpdocs
Remember to add the domains to your hosts file.
Source

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]