apache - 我可以将我网站的资源设为私有吗?
问题描述
我想让我的网站资源(如图像等)变为私有,并且只能由我主机上的本地文件加载。有人能告诉我怎么做吗?
解决方案
如果您有一个单独的图像文件夹,请在您拥有图像的文件夹中创建一个 .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 的想法)
推荐阅读
- python - 解析并对其进行一些更改后,为多部分电子邮件设置的内容类型应该是什么?
- intellij-idea - 地图流 - 误导 Intellij 错误/警告 - 这是一个错误吗?
- javascript - 如何更改事件的目标值(使用 Jest/Enzyme/React 进行单元测试)
- ruby-on-rails - 使用 Rails 仅触发特定操作的某些验证并设计
- html - 按钮被 div 边界切断
- php - php xml dom在结束标记前需要空格`/>`
- reactjs - 如何在使用材质 ui Skeleton 组件做出反应的循环阵列上显示多个加载骨架屏幕?
- python-3.x - Python - requests.get 后跟使用 HashLib 的文件比较
- python - 尽管已安装 matplotlib,但无法导入
- python - 文件未在烧瓶中上传