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.

1 Answer 1


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.
  • 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.
    – MrWhite
    Commented Dec 3, 2012 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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