如何在codeigniter中保护资产文件夹

bra*_*tra 2 php .htaccess mod-rewrite codeigniter

我只想在客户端保护我的资产文件夹。我的所有文件都可以公开访问。我想保护它。我有.htaccess如下规则,那里有错误的规则吗?

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)

Abd*_*lam 5

你可以做两件事

  1. 通过.htaccess(未测试)
  2. 通过 index.html

1..htaccess

.htaccess在assets文件夹内添加

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>
Run Code Online (Sandbox Code Playgroud)

2. index.html

index.html在目录中创建文件并添加这个

<!DOCTYPE html>
<html>
<head>
    <title>403 Forbidden</title>
</head>
<body>

<p>Directory access is forbidden.</p>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

输出

当有人访问外部时,您会在浏览器上获得此信息

禁止目录访问。