Yii2.0url美化,去掉r=site/index

本文介绍如何在Yii2.0框架中通过配置.web.php文件和.htaccess文件来实现URL美化,移除URL中的index.php,并提供基本和高级版本的详细配置示例。

1、basic版本:在config/web.PHP中添加

'urlManager' => [
    'enablePrettyUrl' => true,  //是否启用美化url     'suffix' =>'.html', //伪静态 后缀用html对seo友好,如果启用了这个配置,  //就必须添加扩展名     'showScriptName' => false,  //是否显示脚本名  
    'rules' => [
    ],
],

2、advance版本:在backend/config/main.php中添加以上代码

即可实现 www.test.com/advance/backend/web/index.php/site/login这样的访问方法,再也不用加讨厌的r=site/login了

方法:

Removing index.php from URL in YiiFramework 2.0

To hide the ‘index.php’ and enable the Pretty URL in yiiframework 2.0, this post will help you. For this we have to configure the .htaccess and web.php file.

.htaccess

Please add the following lines in ‘.htaccess’ file inside the ‘web’ directory of yii2.0 application.

1RewriteEngine on
2# If a directory or a file exists, use it directly
3RewriteCond %{REQUEST_FILENAME} !-f
4RewriteCond %{REQUEST_FILENAME} !-d
5# Otherwise forward it to index.php
6RewriteRule . index.php

Configuration of Web.php File

By default ‘config/web.php’ file does not have a option ‘urlManager’. If we want to enable a pretty url, We have to add and configure the ‘urlManager’ in ‘web.php’ file.
To remove the ‘index.php’ from url, we have to the ‘showScriptName’ value as false.
To remove the ‘r’ route variable from url, set the ‘enablePrettyUrl’ value as true.

 

01 

02'urlManager' => [       
03    'class' => 'yii\web\UrlManager',
04        // Disable index.php
05    'showScriptName' => false,
06        // Disable r= routes
07    'enablePrettyUrl' => true,
08    'rules' => array(
09                 
10            ),
11    ],
  

转载于:https://my.oschina.net/botkenni/blog/842291

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值