在Yii2中启用干净的URL [英] Enable clean URL in Yii2

查看:82
本文介绍了在Yii2中启用干净的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Yii2中启用干净的网址.我想删除index.php和'?'从网址参数.为此,需要在Yii2中编辑哪个部分?

How can I enable clean urls in Yii2. I want to remove index.php and '?' from url parameters. Which section needs to be edited in Yii2 for that?

推荐答案

我在yii2中正常工作.为Apache启用mod_rewrite. 对于basic template,请执行以下操作: 在Web文件夹中创建一个.htaccess文件,并将其添加

I got it working in yii2. Enable mod_rewrite for Apache. For basic template do the following: Create a .htaccess file in web folder and add this

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.php中的config文件夹中添加到组件

Then inside config folder, in web.php add to components

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

对于advanced template,在backend/webfrontend/web文件夹中创建.htaccess文件,并在common/config/main.php

In the case of advanced template create the .htaccess files inside backend/web and frontend/web folders and add urlManager component inside common/config/main.php

这篇关于在Yii2中启用干净的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆