Using htaccess, how to restrict seeing directory c

2020-05-16 03:11发布

More specifically, I have an htaccess file that restricts anyone from seeing the directory contents. Such that, nobody can see my 1000s of images in www.example.com/images by using:

deny from all
allow from 127.0.0.1

However, I want to use these images on www.example.com such that, <img src="images/thisimg.jpg" /> works.

I hope I'm in the right direction, but I appreciate any insight/re-direct. This is similar to: How to protect all directory contents, but gain access to php script but I want to link to these images in that directory, and using deny from all does not allow me to do that.

Thanks in advance for your help.

标签: php .htaccess
11条回答
疯言疯语
2楼-- · 2020-05-16 03:46

If it were me I'd just make sure that the referrer is www.example.com, that's kinda one of it's main uses.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-05-16 03:48

All you need to keep people from seeing the directory contents is an index.php or .html file in that folder. Any requests for yoursite.com/images will load index.php, which you'll set to a dummy page.

index.html could be something like:

<html><title>Forbidden!</title><body>Nothing to see here...</body></html>

or a redirect script index.php:

<?php header('Location: /index.php'); exit(); ?>

Don't use .htaccess for blocking directory listings, it blocks access to everything.

查看更多
不美不萌又怎样
4楼-- · 2020-05-16 03:48

in that folder which you want to show access forbidden create a file with name index.php and paste this code

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access This Folder
on this server.</p>
</body></html>

查看更多
倾城 Initia
6楼-- · 2020-05-16 03:50

Use something like this in .htaccess file:

 #
 ###################################################################
 ###                                                             ###
 ###                    Currently protection                     ###
 ###              images (jpg, jpeg, gif, png, bmp)              ###
 ###                       JavaScript (js)                       ###
 ###                Cascading Style Sheets (CSS)                 ###
 ###                                                             ###
 ###################################################################
 #
 <IfModule mod_rewrite.c>
 RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite.com/.*$ [NC]
 RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite.com$ [NC]
 RewriteRule .*\.(jpg|jpeg|gif|png|bmp|js|css)$ http://access-                    denied.us/hotlinks.html [NC,L]
 </ifModule>
 #

This will allow your pages to load the documents and images you want while stoping everyone elses pages from doing it. I use this on all my sites and it works great.

查看更多
登录 后发表回答