无法将prettyUrl应用于新生成的Yii2项目

时间:2015-08-21 19:08:59

标签: php apache .htaccess mod-rewrite yii2

通过Yii教程,我在一开始就遇到了问题。我正在尝试将prettyURL应用于基本的yii2项目。

这是/ web:

下的web.php文件
<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'hCfpup7Utu5ddLMZQoJ6khEkQiwWlp1j',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'urlManager' => [
            'showScriptName' => false,
            'enablePrettyUrl' => true
        ],
        'db' => require(__DIR__ . '/db.php'),
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
}

return $config;

这是我在/ web文件夹中创建的.htacces文件(其中index.php是):

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://snag.gy/gpXz4.jpg

如果我尝试转到另一页:

http://snag.gy/uub3o.jpg

我已经进入XAMPP的httpd.conf并将所有AllowOverides更改为ALL并验证了

LoadModule rewrite_module modules/mod_rewrite.so

未被注释。

1 个答案:

答案 0 :(得分:0)

我有以下配置:

'urlManager' => [
                    'class' => 'yii\web\UrlManager',
                    'enablePrettyUrl' => true,
                    'showScriptName' => false,
                    'rules' => [
                        '' => 'site/index',
                        'login' => 'site/login',
                    ],
                ],

.htacess文件

AddDefaultCharset UTF-8

Options +FollowSymLinks
IndexIgnore */*
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

它适用于我。

修改

你提到了

  

这是/ web:

下的web.php文件

web.php应位于config/web/

可能存在问题。

编辑2

.htaccess文件放在root中,然后尝试localhost/hello localhost/hello/web