1. 程式人生 > >Yii2掉index.php?r=

Yii2掉index.php?r=

nag 記事本 strong 模塊 .html dmi div ttyu 入口

普通

首先確認apache2配置

1. 開啟 apache 的 mod_rewrite 模塊

去掉LoadModule rewrite_module modules/mod_rewrite.so前的“#”符號;

2. 修改 apache 的 AllowOverride

把 AllowOverride None 修改為 AllowOverride All;

在/config/web.php中 ’components‘=>[] 中添加如下代碼:

‘urlManager‘ => [
            ‘enablePrettyUrl‘ => true,
             ‘showScriptName‘ => false,//隱藏index.php 
             //‘enableStrictParsing‘ => false,
             ‘suffix‘ => ‘.html‘,//後綴,如果設置了此項,那麽瀏覽器地址欄就必須帶上.html後綴,否則會報404錯誤
             ‘rules‘ => [
                 //‘<controller:\w+>/<action:\w+>‘=>‘<controller>/<action>‘,
             ],
         ],

  我們還需在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

然後保存在與入口文件index.php同級的目錄下,也就是/web目錄下,文件名自己填.htaccess,文件類型選擇 所有文件 (*.*) ,然後保存即可。

最後測試OK了

高級

修改 advanced/backend/config/main.php 文件如下:

return [
    ‘homeUrl‘ => ‘/admin‘,
    ‘components‘ => [
        ‘request‘ => [
            ‘baseUrl‘ => ‘/admin‘,
        ],
        ‘urlManager‘ => [
            
‘enablePrettyUrl‘ => true, ‘showScriptName‘ => false, ], ], ];

同樣修改 advanced/frontend/config/main.php 文件:

return [
    ‘homeUrl‘ => ‘/‘,
    ‘components‘ => [
        ‘request‘ => [
            ‘baseUrl‘ => ‘‘,
        ],
        ‘urlManager‘ => [
            ‘enablePrettyUrl‘ => true,
            ‘showScriptName‘ => false,
        ],
    ],
];

然後在 advanced/backend/web 目錄中創建 .htaccess 文件,advanced/frontend/web 相同.htaccess 文件 內容如下:

# use mod_rewrite for pretty URL support
RewriteEngine on
# if a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . index.php

至此,配置完畢.

Yii2掉index.php?r=