Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

I think something like this might work:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?YOURDOMAIN\.com [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
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. – w3dk Dec 3 '12 at 8:30

Your Answer


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.