.htaccess 禁止访问目录中的文件

wi3ka0sx  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(170)

我有问题与我.htaccess文件。我想阻止直接访问文件在目录中使用url,但允许通过HTML和PHP代码获取文件。
我的目录结构是/public/media
首先,我在媒体目录中创建.htaccess文件:

order deny,allow
deny from all

但这是行不通的。接下来,我用不同的方法修改了这个文件,例如

<Files ~ "^.*">
  Deny from all
</Files>

RewriteEngine on 
 RewriteCond %{HTTP_REFERER} !^https://(www\.)?,mydomain [NC]
 RewriteCond %{HTTP_REFERER} !^https://(www\.)?mydomain.*$ [NC]
 RewriteRule \.(gif|jpg|png|bmp|jpeg)$ - [F]

不幸的是,这些代码都不起作用。
根目录中的Main .htaccess

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

Options -Indexes

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

</IfModule>
ux6nzvsh

ux6nzvsh1#

最好的方法是使用Amazon S3之类的服务和临时URL https://laravel.com/docs/9.x/filesystem#temporary-urls

相关问题