Using .htaccess to deny directory access while allowing acce

Connect with other users about what to run on your webhosting (and how to run it) here.
Post Reply
User avatar
coupsan
A semi-regular
A semi-regular
Posts: 26
https://www.youtube.com/channel/UC40BgXanDqOYoVCYFDSTfHA
Joined: Thu Jul 16, 2009 10:10 pm

Using .htaccess to deny directory access while allowing acce

Post by coupsan »

I'd like to use .htaccess to deny entry to particular directories while still allowing access to the files contained in them if directly linked to. In my case, I have a directory that contains a lot of images, but I'd like to prevent people from seeing those images unless I link to them directly. I've tried googling this but the results tend to lead directly to Apache's reference site which is honestly kind of a pain to read.
User avatar
kraze
Former staff
Former staff
Posts: 4362
Joined: Fri Sep 17, 2010 9:06 am
Location: California

Re: Using .htaccess to deny directory access while allowing

Post by kraze »

You are indeed in the right location. I came across the answer to your problem because I also host a lot of content on my site and do not like people being able to browse directories. I am looking for the line to add to your .htaccess file as I type..

Hopefully I will find it soon..
@Kraze^NFo> Juski has a very valid point
@Juski> Got my new signature, thanks!
@Kraze^NFo> Out of context!
@Juski> Doesn't matter!
@Juski> You said I had a valid point! You can't take it back now! It's out there!
User avatar
kraze
Former staff
Former staff
Posts: 4362
Joined: Fri Sep 17, 2010 9:06 am
Location: California

Re: Using .htaccess to deny directory access while allowing

Post by kraze »

This should help you..
RewriteCond %{HTTP_REFERER} !yoursite.com [NC]
RewriteRule .* - [F]

Your going to need to customize it of course.
@Kraze^NFo> Juski has a very valid point
@Juski> Got my new signature, thanks!
@Kraze^NFo> Out of context!
@Juski> Doesn't matter!
@Juski> You said I had a valid point! You can't take it back now! It's out there!
User avatar
Edge100x
Founder
Founder
Posts: 12961
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Using .htaccess to deny directory access while allowing

Post by Edge100x »

If you use mod_rewrite, you will most likely need this at the top, as well:

RewriteEngine on
User avatar
coupsan
A semi-regular
A semi-regular
Posts: 26
Joined: Thu Jul 16, 2009 10:10 pm

Re: Using .htaccess to deny directory access while allowing

Post by coupsan »

Not sure what exactly I'm supposed to customize in those particular lines. The file works--except it also forbids access to the images.
User avatar
Edge100x
Founder
Founder
Posts: 12961
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Using .htaccess to deny directory access while allowing

Post by Edge100x »

Rereading this, are you just trying to make it so the directory listing doesn't show up? If so, the simplest way is to create a blank index.html file in the folder.

The solution that kraze was giving was meant to defend against external linking.
User avatar
coupsan
A semi-regular
A semi-regular
Posts: 26
Joined: Thu Jul 16, 2009 10:10 pm

Re: Using .htaccess to deny directory access while allowing

Post by coupsan »

Ahaha yes that is what I wanted to do, woops, I can't believe I overcomplicated things that badly. WELP, thanks!
Post Reply