在 Yii2 中启用干净的 URL [英] Enable clean URL in Yii2
问题描述
如何在 Yii2 中启用干净的 url.我想删除 index.php 和 '?'从 url 参数.需要在 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
.对于基本模板
,请执行以下操作:在 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
然后在 config 文件夹中,在 web.php 中添加到组件
Then inside config folder, in web.php add to components
'urlManager' => [
'class' => 'yiiwebUrlManager',
// 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>',
),
],
在高级模板
的情况下,在backend/web
和frontend/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屋!