简单的URL重写失败

时间:2015-07-11 12:06:56

标签: .htaccess url-rewriting

我有一个非常简单的目标:重写我的PHP应用程序的URL,以便系统将localhost/slim_demo/archive解释为localhost/slim_demo/index.php/archive,但用户会看到前者。 编辑:系统的行为就像没有重写一样。后一版本的URL返回数据,但前者抛出Not Found错误。

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

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/$1 [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>

不确定我还需要做什么!

1 个答案:

答案 0 :(得分:2)

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

  

在Directory和htaccess上下文中,Pattern最初会是   删除前缀后,与文件系统路径匹配   将服务器引导到当前的RewriteRule

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

这意味着当您尝试获取网址localhost/slim_demo/archive时,slim_demo部分会被删除,而您的规则永远无法匹配。

所以你需要:

RewriteRule ^(.*)$ index.php/$1

但这会带来无限循环和500错误。仅当REQUEST_URI没有index.php时才必须触发此规则。

一起变成:

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