简单 URL 重写失败

Simple URL rewriting fails

我有一个非常简单的目标:重写我的 PHP 应用程序的 URL,这样 localhost/slim_demo/archive 被系统解释为 localhost/slim_demo/index.php/archive 但用户看到前者。 编辑:系统表现得好像没有发生重写。 URL returns 数据的后一版本,但前者抛出 Not Found 错误。

我使用了下面的 .htaccess 文件,但它没有发生(顺便说一句,正如第二行所说,取消注释它会拒绝所有请求,这表明 .htaccess 还活着并且正在踢) :

Options +FollowSymLinks -MultiViews -Indexes 
#deny from 127.0.0.1 #Uncomment to prove that .htacess is working
RewriteEngine On
RewriteRule ^slim_demo/(.*)$ slim_demo/index.php/ [NC,L]

这是我 apache2.conf 的相关部分:

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>
<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>
<Directory /media/common/htdocs>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

我也做了 a2enmod rewriteservice apache2 restart。沮丧的是,我也将其添加到我的可用站点并重新启动:

<Directory /media/common/htdocs/>
        Options +FollowSymLinks -Indexes
        AllowOverride All
</Directory>

不确定我还需要做什么!

因此,如果此 .htaccess 文件位于 slim_demo 目录中,则您的 RewriteRule 永远不会匹配:

In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule

(模式在您的情况下是 ^slim_demo/(.*)$ 部分)。

这意味着当您尝试获取 URL localhost/slim_demo/archive 时,slim_demo 部分将被删除,并且您的规则永远无法匹配。

所以你需要:

RewriteRule ^(.*)$ index.php/

但这会使您进入无限循环和 500 错误。仅当 REQUEST_URI 没有 index.php.

时才必须触发此规则

合起来变成:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(?!/slim_demo/index\.php).*$
RewriteRule ^(.*)$ index.php/ [NC,L,QSA]