简体   繁体   中英

How to prevent direct access to files but allow files in webpages

I'm trying to use mod.rewrite to deny direct access to files on my web server, eg http://domain.tld/reports/imareport.pdf or http://domain.tld/img/img1.png , and I've used the answer on this question:

(htaccess) How to prevent a file from DIRECT URL ACCESS?

That page suggests using mod.rewrite like this:

RewriteEngine on 
RewriteRule \.(png|pdf|htm)$ - [F]

Using mod.rewrite in this manner works fine for denying access to PDFs, but other files that are ordinarily included in a page such as images and css are not only blocked from direct access, but also blocked when used on a webpage in a normal <img> tag or whatever. This is contrary to the question and answer mentioned above.

So... my question is... is there a way to block direct access to files but still allow them in webpages?

Thanks Mark Phillips, I didn't fully appreciate what these two rewrite conditions were doing for me:

RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]

So I had managed to mess them up. Things worked as needed when I used the code just as it was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM