Yii 2 修改 URL 模式为 PATH 模式,并隐藏index.php

美化URL很重要,URL 看起来顺眼,JJ就不疼。废话不多说,两步搞定。
第一:在配置文件加上 urlManager项,看代码:

 'urlManager'=>[
    'class' => 'yiiwebUrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules'=>[
	'<controller:(post|comment)>/<id:d+>/<action:(create|update|delete)>' =>'<controller>/<action>',
	'<controller:(post|comment)>/<id:d+>' => '<controller>/read',
	'<controller:(post|comment)>s' => '<controller>/list',
	     ],
     ],

第二,在index.php同级目录下新建.htaccess文件,把下面的代码贴上:

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

好了,就是这么轻松。我是YiiSoEasy,胖纸囧是我小弟。

原文地址:https://www.cnblogs.com/handongyu/p/6389044.html