如何从Apache / Centos中的Yii URL中删除index.php?

时间:2013-05-27 11:20:47

标签: apache .htaccess mod-rewrite yii

我正在 Apache / 2.2.15(CentOS)服务器 上使用 Yii Framework


以下行已在 /etc/httpd/conf/httpd.conf

中取消注释
LoadModule rewrite_module modules/mod_rewrite.so


当我执行以下操作时,我可以在已加载模块下看到 mod_rewrite

<?php phpinfo(); ?>


Yii项目结构:

/var/www/test/ 
/var/www/test/index.php 
/var/www/test/.htaccess


.htaccess内容

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php


我这样做时效果很好:

http://10.20.30.40/test/index.php/testcontroller/testaction


但是当我这样做时:

http://10.20.30.40/test/testcontroller/testaction

显示以下错误:

Not Found

The requested URL /var/www/test/index.php was not found on this server.

有什么想法吗?

4 个答案:

答案 0 :(得分:4)

我遇到同样的麻烦。 我使用Apache配置别名,例如:

<VirtualHost *:80>
...

Alias /project "/Users/foo/Sites/project"

...
</VirtualHost>

要解决,我在.htaccess上使用“RewriteBase”指令,例如:

RewriteEngine on
RewriteBase /project # <---- Modify here! and remove this comment.

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

来源:http://www.yiiframework.com/wiki/214/url-hide-index-php/#c9725

答案 1 :(得分:2)

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

htaccess内容。

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => false,
    'rules' => array(
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>'),
你的main.php中的

urlmanager配置。或者,您可以根据需要自定义它。

答案 2 :(得分:2)

确保httpd.conf中项目的web目录的AllowOverride为“All”。如果没有,请更改该值并重新启动Web服务器

答案 3 :(得分:0)

我解决了这个编辑Apache配置文件的问题: {{ApacheDir}}/conf/httpd.conf
并取消评论/添加以下行: LoadModule rewrite_module modules/mod_rewrite.so