Yii2 Apache + Nginx 路由重寫

來源:互聯網
上載者:User

標籤:apach   man   sof   表示   document   config   ide   include   span   

一、什麼是路由重寫

  原本的HTTP訪問地址: www.test.com/index.php?r=post/view&id=100  表示這個請求將由PostController 的 actionView來處理。

  重寫後的HTTP訪問地址 www.test.com/post/view/id/100  這樣的連結看起來簡潔美觀,對於使用者比較友好。同時,也比較適合搜尋引擎的胃口, 據說是SEO的手段之一。

二、Apache 路由重寫

  (一)開啟Apache的重寫模組

    1. 開啟apache的config的 hhttpd.conf 將 #LoadModule rewrite_module modules/mod_rewrite.so  的#號去掉

    2. 重啟Apache伺服器即可

  (二)虛擬機器主機配置

    1. 開啟 Apache\conf\vhosts.conf配置路徑

    2. 將配置修改為如下:

<VirtualHost 127.0.0.1:80>    ServerName test-yii2.com    DocumentRoot F:/wamp64/www/yii2/frontend/web    <Directory  "F:/wamp64/www/yii2/frontend/web">        Options +Indexes +Includes +FollowSymLinks +MultiViews        AllowOverride All        Require all granted        RewriteEngine on        # 如果請求的是真實存在的檔案或目錄,直接存取        RewriteCond %{REQUEST_FILENAME} !-f        RewriteCond %{REQUEST_FILENAME} !-d        # 如果請求的不是真實檔案或目錄,分發請求至 index.php        RewriteRule . index.php    </Directory></VirtualHost>

    3. 重啟Apache

  (三)還有另外一種配置方式,無需重啟Apache伺服器,當時改當時生效。

    1. 在 “F:/wamp64/www/yii2/frontend/web”  目錄下增加 .htaccess 代碼如下:    

    RewriteEngine on    # 如果請求的是真實存在的檔案或目錄,直接存取    RewriteCond %{REQUEST_FILENAME} !-f    RewriteCond %{REQUEST_FILENAME} !-d    # 如果請求的不是真實檔案或目錄,分發請求至 index.php    RewriteRule . index.php

  (四) YII配置

    1. 在main.php下 components這個數組中增加如下如下配置:

        ‘urlManager‘ => [            ‘enablePrettyUrl‘ => true,  //開啟美化url配置,預設關閉            ‘enableStrictParsing‘ => false, //不啟用嚴格解析,預設不啟用.如果設定為true,則必須建立rules規則,且路徑必須符合一條以上規則才允許訪問            ‘showScriptName‘ => false, //隱藏index.php            ‘rules‘ => [               // http://frontend.com/site/index 重寫為  http://frontend.com/site                ‘<controller:\w+>/‘=>‘<controller>/index‘,            // http://frontend.com/site/view?id=1 重寫為 http://frontend.com/site/1                ‘<controller:\w+>/<id:\d+>‘ => ‘<controller>/view‘,            // http://frontend.com/site/ceshi?id=123 重寫為  http://frontend.com/site/ceshi/123                ‘<controller:\w+>/<action:\w+>/<id:\d+>‘ => ‘<controller>/<action>‘,            ]        ],

 

 

    

 

Yii2 Apache + Nginx 路由重寫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.