我想使用.htaccess
文件来检查请求的路径是否是public/
目录中的文件。如果是,请提供,否则转发请求/index.php
。我似乎无法让这个工作。
这就是我所拥有的:
Options +FollowSymLinks
RewriteEngine on
Options -Indexes
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/public%{REQUEST_URI} [L]
RewriteRule ^ index.php [QSA,L]
e.g。 http://example.com/css/style.css
应该让apache服务/public/css/style.css
,因为它是一个存在的文件,但http://example.com/css/style.bad
应该发送到/index.php
。
答案 0 :(得分:6)
此代码应该在您身边按预期工作
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{THE_REQUEST} \s/public/ [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L,QSA]
答案 1 :(得分:2)
显然是[L]
does not work as I expected。考虑到这一点,以及大量的反复试验,我设法找到了有效的方法:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ public%{REQUEST_URI} [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^ index.php [QSA,L]