1

我有一个基于 Slim 框架的 PHP 应用程序,在/public目录和以下文件中有静态.htacces文件:

# URL routing
Options -MultiViews
RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/public%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

当我尝试访问 /index.php 时,Apache 直接返回它,但我希望它被传递以重写为 index.php(这将返回 404 未找到)。我该怎么做?

如果我尝试删除RewriteCond %{REQUEST_FILENAME} !-f

# URL routing
Options -MultiViews
RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/public%{REQUEST_URI} [L]

RewriteRule ^ index.php [QSA,L]

Apache 向我返回“内部服务器错误”。我怎样才能完成它?

更新:我的项目结构

www/
├── app/
├── cache/
├── public/
│   ├── css/
│   ├── img/
│   └── js/
├── vendor/
├── views/
└── index.php
4

2 回答 2

1

你的 RewriteRules 是一团糟。

RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/public%{REQUEST_URI} [L]

你告诉 Apache 有“如果 DOCUMENT_ROOT/public%{REQUEST_URI} 是一个文件”。但是,在您的设置中,DOCUMENT_ROOT 已经是 public/. 这是你乱七八糟的根源。

考虑一下:

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.+)$ /public/$1 [L]

 RewriteRule ^$ ./index.php [QSA,L]

Apache 已经为您进行了文件名翻译,这是您尝试使用 DOCUMENT_ROOT 进行的翻译

于 2013-05-26T15:17:00.687 回答
-1

最好的办法是删除 .HTACCESS 文件或在您的服务器根目录中编辑它

DirectoryIndex public_html www html

于 2013-05-26T15:15:52.790 回答