YII框架中的路由優化

本來訪問路由框架的url是:php

    http://www.myyii.com/web/index.php?r=hello/list;html

可是這樣的路由對於SEO太過於不友好;web

 

第一步:去掉路由中的webapache

             解決辦法:配置虛擬主機的時候,將虛擬主機的根目錄直接指向到項目的web目錄下便可;致此,路由變成了 http://www.myyii.com/index.php?r=hello/list。框架

 

第二部:去掉路(隱藏)由中的index.phpyii

            解決辦法:性能

            1.開啓apache配置的路由重寫模塊;優化

            2.修改urlManager組件中的'showScriptName'配置項:url

                  'showScriptName' => false, //隱藏入口腳本htm

此時,路由變成了 http://www.myyii.com/?r=hello/list;

 

第三部分:自定義路由規則,實現完全優化

         解決辦法:

                      修改urlManager組件中的'rules'配置項,在其中定義本身想要的路由規則;如:

     'urlManager' => [

             //'suffix' => '.html', //假後綴
            'enablePrettyUrl' => true, //路由路徑化
            'showScriptName' => false, //隱藏入口腳本
            'rules' => [
                'list' => 'hello/list', /沒有參數

                'list/<p:\d+>' => 'hello/list',//有一個參數p

                'test/<p:\d+>/<id:\d+>/<test:\w+>' => 'hello/test',//有三個參數p、id、test
            ],
        ],

 

注:

 1. .htaccess文件是跟隨入口文件,必須和入口文件處於同一級目錄;

 2. .htaccess 當訪問量高了之後會很影響性能:放在文件中讀取的是磁盤,放在配置文件中讀取的是內存;如:

<VirtualHost *:80>
    ServerAdmin xiongjiangbo
    DocumentRoot "d:/workFile/myyii/web/"
    ServerName www.myyii.com
    ErrorLog "logs/dummy-host2.example.com-error.log"
    CustomLog "logs/dummy-host2.example.com-access.log" common
    
    <Directory "d:/workFile/myyii/web/">
        Options +FollowSymlinks
        RewriteEngine On

        RewriteCond %{REQUEST_FILENAME} !-d         RewriteCond %{REQUEST_FILENAME} !-f         RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]     </Directory>      </VirtualHost>

相關文章
相關標籤/搜索