Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I want to restrict access to a folder on my server so that visitors may only access the contents (a web application) via links in the same domain. Can I do this using .htaccess? To be clear, I simply want to prevent direct access to the contents so that visitors are routed through other pages on my website in order to get there.

share|improve this question
add comment

1 Answer

I think something like this might work:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?YOURDOMAIN\.com [NC]
RewriteCond %{REQUEST_URI} ^/PROTECTEDFOLDER/.*$ [NC]
RewriteRule ^(.*)$ "http\:\/\/www\.YOURDOMAIN\.com\/$1" [R=301]

I'm haven't tried this, but it should work. Some explanation:

  • If the referrer is not from your domain, or a subdomain...
  • If the folder "PROTECTEDFOLDER" is being accessed...
  • Redirect to your homepage.
share|improve this answer
1  
It should be noted, however, that the HTTP_REFERER is dependent on the clients browser, so is inherently unreliable and can be faked. So you could find that legitimate users are unable to access your app and 3rd party sites can access your app anyway. This might only affect a small percentage, but it's something to be aware of. –  w3d Dec 3 '12 at 8:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.