首页 > 解决方案 > 我可以将我网站的资源设为私有吗?

问题描述

我想让我的网站资源(如图像等)变为私有,并且只能由我主机上的本地文件加载。有人能告诉我怎么做吗?

标签: apache.htaccesswebhosting

解决方案


如果您有一个单独的图像文件夹,请在您拥有图像的文件夹中创建一个 .htaccess 文件,并将以下内容添加到该 .htaccess 文件中:

Require local #Make sure there is no file in the directory

要隐藏所有目录中的大多数图像,请将其添加到根 .htaccess 文件中:

<FilesMatch "\.(jpeg|jpg|png|webp|gif)$">
Require local
</FilesMatch>

我更喜欢第二个,因为它的灵活性。

或者,如果您具有 root 访问权限(如果您在 VPS/或您自己的服务器上),则使用以下命令:

打开 httpd.conf 或 apache2.conf(其中任何一个),并添加以下内容:

<Directory "/path/to/directory/with/images">
Require local
</Directory>

或者,如果包含图像的目录还包含需要公开的文件,请执行以下操作:

<Directory "/path/to/directory/with/images">
<FilesMatch "\.(jpeg|jpg|png|webp|gif)$">
Require local
</FilesMatch>
</Directory>

或者,您可以将图像文件移出 DOCUMENT_ROOT (@arkascha 的想法)


推荐阅读