繁体   English   中英

漂亮的网址在服务器yii2上不起作用

[英]Pretty urls is not working on the server yii2

我使用urlManager配置来获得一个漂亮的url:

   'urlManager' => [
                'class' => 'yii\web\UrlManager',
                'enablePrettyUrl' => true,
                'showScriptName' => false,
                'enableStrictParsing' => false,
                'rules' => [
                    'login/' => 'site/login',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>/',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>/',
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                ],
            ],

它在我的本地主机上工作,但在服务器上不工作。 .htaccess文件内容:

# Increase cookie security
<IfModule php5_module>
  php_value session.cookie_httponly true
</IfModule>

# Settings to hide index.php and ensure pretty urls
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

重要的事情要做:

Recent Apache versions (from 2.3.9) have "AllowOverride None" by default and versions before had "AllowOverride All" (see allowoverride).

因此该代码可以在localhost上运行,但由于不同的Apache版本而在服务器上提供404。

http://www.yiiframework.com/forum/index.php/topic/51470-pretty-urls-and-htaccess

在YII2 Advance模板中启用Pretty URL。

首先在项目目录的根目录下创建一个.htaccess文件,例如,如果您的项目目录名称是“ Yii-Advance”,则制作一个文件Yii-Advance / .htaccess并在其中插入以下代码。

第1步

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>

    <IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} ^/(admin)
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]
RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/
RewriteCond %{REQUEST_URI} ^/(admin)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} ^/(assets|css)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/
RewriteCond %{REQUEST_URI} !index.php


 RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ frontend/web/index.php
</IfModule>

第2步

使用此代码创建一个名为“ Request.php”的新文件,并将其放在“ common / components /”下。

     <?php

    namespace common\components;
class Request extends \yii\web\Request {
    public $web;
    public $adminUrl;

    public function getBaseUrl(){
        return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;
    }


/*
    If you don't have this function, the admin site will 404 if you leave off
    the trailing slash.

    E.g.:

    Wouldn't work:
    site.com/admin

    Would work:
    site.com/admin/

    Using this function, both will work.
*/
public function resolvePathInfo(){
    if($this->getUrl() === $this->adminUrl){
        return "";
        }else{
            return parent::resolvePathInfo();
        }
    }
}

第三步

现在安装将这些行放在frontend / config / main.php下

  'request'=>[
    'class' => 'common\components\Request',
    'web'=> '/frontend/web'
],

第四步

将这些行放在backend / config / main.php下

  'request'=>[
    'class' => 'common\components\Request',
    'web'=> '/backend/web',
    'adminUrl' => '/admin'
],

第6步

以这种方式在Frontend / config / main.php和backend / config / main.php中启用Pretty URL,在我的情况下,我有两个控制器“ Site,User”

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<alias:index|login|logout|contact|about|signup|request-password-reset|reset-password>' => 'site/<alias>',
            '<alias:index|create|confirm|confirmation|update|delete>' => 'user/<alias>',
        ],
    ],

而已

现在,当您访问yii2高级模板应用程序时,您将以这种方式看到url

在执行这些步骤之前,URL为: http:// localhost / Yii-Advance / frontend / web / site / login

这些步骤之后,URL为: http:// localhost / Yii-Advance / login

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM