Yii urlManager设置导致404 Not Found

时间:2016-01-27 01:09:39

标签: apache yii

网址localhost / photogallery有效,但每个链接都会生成404。

顺便说一下,在更改urlManager的设置之前,链接工作正常。

示例:

Not Found

The requested URL /photogallery/site/page was not found on this server.
Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80

在我修改config文件夹中的main.php之前它已经工作了。

的.htaccess

 Options +FollowSymLinks
 IndexIgnore */*

 <IfModule mod_rewrite.c>
 RewriteEngine on

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

 RewriteRule ^.*$ /index.php [L] 
 </IfModule>

main.php

<?php

// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');

// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
        'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
        'name'=>'My Web Application',

        // preloading 'log' component
        'preload'=>array('log'),

        // autoloading model and component classes
        'import'=>array(
                'application.models.*',
                'application.components.*',
        ),

        'modules'=>array(
                // uncomment the following to enable the Gii tool

                'gii'=>array(
                        'class'=>'system.gii.GiiModule',
                        'password'=>'allo',
                        // If removed, Gii defaults to localhost only. Edit carefully to taste.
                        'ipFilters'=>array('127.0.0.1','::1'),
                ),

        ),
    // application components
    'components'=>array(

            'user'=>array(
                    // enable cookie-based authentication
                    'allowAutoLogin'=>true,
            ),

            // uncomment the following to enable URLs in path-format

            '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>',
                ),
            ),


            // database settings are configured in database.php
            'db'=>require(dirname(__FILE__).'/database.php'),

            'errorHandler'=>array(
                    // use 'site/error' action to display errors
                    'errorAction'=>YII_DEBUG ? null : 'site/error',
            ),

            'log'=>array(
                    'class'=>'CLogRouter',
                    'routes'=>array(
                            array( 
                                    'class'=>'CFileLogRoute',
                                    'levels'=>'error, warning, trace',
                            ),
                            // uncomment the following to show log messages on web pages

                            array(
                                    'class'=>'CWebLogRoute',
                                    'levels'=>'error, warning, info',
                            ),

                    ),
            ),

    ),

    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>array(
            // this is used in contact page
            'adminEmail'=>'webmaster@example.com',
            'uploads'=>'/uploads',    
    ),
);

我想知道是否需要执行任何特定配置才能在默认的Apache服务器配置上运行它。我不想更改设置,因为我使用了许多其他php框架而且我不想破坏它。

将.htaccess更改为:

 Options +FollowSymLinks
 IndexIgnore */*

 <IfModule mod_rewrite.c>
 RewriteEngine on

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

 RewriteRule .* index.php/$0 [PT,L] 
 </IfModule>

仍然无法正常工作。

1 个答案:

答案 0 :(得分:1)

试试这个

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine on

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

RewriteRule .* index.php/$0 [PT,L]

删除.htaccess中的<IfModule mod_rewrite.c> </IfModule>

如果出现错误500或服务器错误,请刷新页面。然后问题是你的apache重写引擎被禁用了。

enter image description here

如果你收到此错误,请查找apache的httpd.conf。

然后查找这行代码

#LoadModule rewrite_module modules/mod_rewrite.so

删除#,取消注释这行代码。然后别忘了重启你的apache。

检查此链接:

如何启用mod_rewrite http://forum.wampserver.com/read.php?2,30758,30758

Yii网址管理 http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x

如何从URL中删除index.php http://www.yiiframework.com/forum/index.php/topic/7803-how-to-remove-index-php-from-url/