看起来好像有一些错误。
它看起来像你的RewriteCond的倒退。如果%{DOCUMENT_ROOT}/public/%{REQUEST_URI}
没有存在(!-f),那么你想重写index.php,但是如果它确实存在(-f),那么重写为/ public/$ 1。第二件事RewriteRule ^.*$ - [L]
实际上是阻止应用实际规则,因为它以[L]结束,并且停止当前迭代中的重写。
即使你删除^.*$ - [L]
重写和翻转-f
和!-f
,运行与重写的第二个迭代一个问题:
RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} !-f
RewriteRule ^.*$ index.php [L]
RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/public/$1 [L]
这是发生了什么,当您尝试访问http://example.com/foo/bar:
- %{DOCUMENT_ROOT} /公共//富/酒吧不存在,!-f条件满足
- 富/栏改写成的index.php,与[L],端读和写伊特
- 该请求被重定向INTERNALLY到index.php
- 随着新的URI(的index.php)所有规则重新应用
- %{DOCUMENT_ROOT} /public/index.php存在,!-f条件失败
- %{DOCUMENT_ROOT} /public/index.php存在,-f条件满足
- index.php文件被改写为%{DOCUMENT_ROOT} /public/index.php
- 内部重定向,并且所有规则都将重新应用到新的URI(/public/index.php)
- %{DOCUMENT_ROOT} /public//public/index.php do esn't存在,!-f条件满足
- 公共/ index.php文件被改写到index.php
- 回去3.内部循环,当您尝试访问http://example.com/images/foo.gif
同样的事情也发生了,基本上,你需要得到另一个规则来停止第二次重写。所以你需要添加第二组条件:
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} !-f
RewriteRule ^.*$ index.php [L]
RewriteCond %{REQUEST_URI} !/index.php
RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/public/$1 [L]