Yii2从url中删除index.php

时间:2015-06-01 21:39:34

标签: php yii yii2

当我在Yii中设置选项以从URL中删除index.php时,我收到404错误,并在错误日志File does not exist: /var/live/var中出现此错误。在我的浏览器中,我收到此错误The requested URL /var/decat/frontend/web/index.php was not found on this server.,但文件正好在该位置。可以解释的是,我的文档根目录是/var/live,decat是别名,如conf文件中所示。

此网址工作正常http://130.211.165.180/decat/index.php/site/login,但当我删除index.php时,我收到错误。我按照所有说明在conf文件中进行设置。我甚至尝试过.htaccess文件。这是我的conf文件中的信息。

Alias /decat /var/decat/frontend/web

<Directory "/var/decat/frontend/web">
        # use mod_rewrite for pretty URL support
        RewriteEngine on
        # If a directory or a file exists, use the request directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Otherwise forward the request to index.php
        RewriteRule . index.php 
        Options -Indexes FollowSymLinks 
        AllowOverride All
        Order allow,deny 
        Allow from all 
</Directory>

5 个答案:

答案 0 :(得分:7)

RewriteBase /decat/之后添加RewriteEngine on并重新启动apache。

答案 1 :(得分:4)

您应该像这样设置Url Manager组件:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false, // Only considered when enablePrettyUrl is set to true
],

官方文档:

答案 2 :(得分:3)

yii2从网址中删除网页

1)使用

中的以下内容编辑config / web.php文件
<?php
use \yii\web\Request;

$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());

return [
    ...
    'components' => [
            'request' => [
                'baseUrl' => $baseUrl,
     ],
      ...
    ]
]
?>

2)将以下htaccess添加到root / web

RewriteEngine On RewriteBase /

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

RewriteRule . index.php

3)将以下htaccess添加到安装了应用程序的根文件夹

# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)?$ web/$1

enter image description here

答案 3 :(得分:1)

您应该将.htaccess更改为

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?r=$1 [L,QSA]

和urlManager规则为

 'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Disable index.php
    'showScriptName' => false,
    // Disable r= routes
    'enablePrettyUrl' => true,
    'rules' => array(
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
    ],

我在我的应用中使用此功能。它工作。 我的网址为 www.example.com/user/view?id=1

答案 4 :(得分:0)

我认为这是你的Apache配置错误...你告诉它只在我读它时重写单个字符的请求?

我认为需要:

RewriteRule ^/(.*) index.php/$1 [L]

但我错了,不是apache专家