Apache Mod 重写前端控制器模式

Apache Mod Rewrite For Front Controller Pattern

我的 .htaccess 文件中有以下内容:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ _service.php?uri= [NC,L]
</IfModule>

唯一的问题是没有发生重写,当我试图访问服务器的根页面时,我只是看到一个目录列表 localhost

即使我尝试将匹配设置为可选 RewriteRule ^(.*)?$ _service.php?uri= [NC,L] 它也不起作用。

重写似乎适用于任何其他情况,例如localhost/foo/bar

如何让重写发生在这种特殊情况下?

问题是根是一个目录。因此,这种情况会阻止应用您的规则:

RewriteCond %{REQUEST_FILENAME} !-d

尝试添加如下规则:

RewriteRule ^$ _service.php?uri=/ [NC,L]

什么的

您可能需要指定文档根目录以便找到文件或目录;我对这种模式的变化是:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteRule . /index.php [L,QSA,B]

在这种情况下(例如 http://www.example.com/),根页面(对我而言)是正确的。注意,我在 vhost.conf 中有这个,而不是在 .htaccess.