Linux Blog

htaccess allow from

Filed under: General Linux — TheLinuxBlog.com at 10:32 am on Tuesday, September 9, 2008

htaccess allow from gives you the ability to allow (or deny) specific IP’s or domain names from a directory on your server. To do this the syntax is quite simple. Using VIM or nano open up the .htaccess file in the directory that you want to restrict access to. You need to add the following:

Order Deny,Allow
Deny from all
Allow from 127.0.0.1 <ip></ip>

This allows access from your local host and the IP address you specify. Using .htaccess you can also allow by host name. This is useful if you wish to allow or deny a friend access to a directory. (note: it will also work if you have them in your hosts file)

Order Deny,Allow
Deny from all
Allow from LinuxBlog
Allow from .thelinuxblog.com

Using htaccess to allow from your LAN is also pretty easy. You use your CIDR address (ip/subnet) to do this try something like this (changing to match your LAN):

Order Deny,Allow
Deny from all
Allow from 192.168.1.1/24

I run into htaccess allow problems a lot, and hope that this will clear the air up for me. htaccess can be very handy if you do not want to keep turning your firewall on and off, but do not want your directories wide open. Just remember, if you want to stop everyone except those you choose to access your apache web directories, use htaccess allow from!





Random Man Pages:
r128
osx
vmsplice
termio

8 Comments »

Comment by livecam

March 14, 2009 @ 2:38 pm

great!!! really only three lines for satisfaction. i searched really long for this. thx

Comment by Marcel Korpel

January 24, 2010 @ 10:39 pm

Although your text is very clear, I am not able to get this magic to work. In my .htaccess there is


Order Deny,Allow
Deny from all
Allow from MY.IP.WAS.HERE

But neither I nor another server was able to access test.php, we all got a 403-error. I changed my IP address to only “94″, “94.*.*.*” and “.ziggo.nl”, but nothing works.

What am I doing wrong?

Comment by Marcel Korpel

January 24, 2010 @ 10:41 pm

Whoops, some tags in my .htaccess were eaten. Here is the right file:

<Files test.php>
Order Deny,Allow
Deny from all
Allow from MY.IP.WAS.HERE
</Files>

Comment by Sergiu

January 24, 2011 @ 11:46 am

Yep, I’m geting the same problem as Marcel. I think it’s a host related settings problem. Can’t use neither htpwd nor othe functions in htacces.

Still looking for a solution though.

Comment by TheLinuxBlog.com

January 24, 2011 @ 11:48 am

I’ve had to put in a support ticket with a hosting service regarding a similar issue before. It’s worth a shot.

Comment by Revelations

May 2, 2011 @ 2:25 am

Very handy site.
Just a side note, its very much the same when you are doing directories:

Order Deny,Allow
Deny from all
Allow from 192.168.0.0/24
Allow from Any.Ip.ad.dress/23

Also from the above if you want to add multi subnets each of them need to be on a separate line.

Comment by Revelations

May 2, 2011 @ 2:27 am


Order Deny,Allow
Deny from all
Allow from 192.168.0.0/24
Allow from Any.Ip.ad.dress/23

Comment by Victor

August 16, 2011 @ 8:45 pm

It did not work for me. I add the .htaccess to my phpmy admin folder with

Order Deny,Allow
Deny from all
Allow from 192.168.5.31/45

and I still can access the folder from outside usign my global IP

any suggestions?

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>