jon
jon

Reputation: 1581

Forbidden access to files in a specific folder

I have a directory files that contains files that I don't want to allow to be accessed directly If you go to www.mywebiste.com/files/myfile.pdf then you would be redirected.

However I want the files to be accessible from the rest of the site. ie. I may have a page www.mywebiste.com/dave/page/ that needs to be able to display files from within the files dir.

I have tried the following htacccess in the files dir, but it's not working:

RewriteEngine ON
RewriteCond %{HTTP_REFERER} ^http://www.mywebiste.com/files/ [NC]
RewriteRule ^.*$ - [R=404,L]

Upvotes: 2

Views: 393

Answers (1)

Prix
Prix

Reputation: 19528

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_REFERER} ^$
RewriteRule ^files/.*$ - [F]

You may also use:

RewriteCond %{HTTP_REFERER} !^http://(www.)?mywebiste.com/.*$ [NC]
RewriteRule ^files/.*$ - [F]

Since the refer comes from the page it was called the page could be anything within your domain so I simple use ^http://(www.)?mywebiste.com/.*$ you may as well use ^http://(www.)?mywebiste.com if you feel more comfortable with. If refer is empty and folder is files deny access to it.

PS: http://(www.)?mywebiste.com means either site address have www. or doesn't.


As for robots you could use user agent to allow access even when the refer is empty.

robots.txt example:

User-agent: *
Disallow: /files/

More information about robots.txt see here.

Upvotes: 4

Related Questions