Search code examples
.htaccesshotlinking

Preventing hotlinking of certain filetypes in certain directories


I'd like to prevent hotlinking of pdfs in a certain website directory without imposing a site-wide ban, and redirect any direct traffic to a given page. I suspect htaccess is my friend for this but don't know how I should define these rules.


Solution

  • Try:

    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^https?://example\.com/ [NC]
    RewriteRule ^folder/ /given-page.html [L,R]
    

    Here, any request for anything in the folder /folder/ that is not referred to by your site (example.com) will be redirected to /given-page.html.