Yii2: pretty urls

Yii2: pretty urls

โดยปกติ url ของ yii2 จะเป็นแบบ http://localhost/BanBan/1/backend/web/index.php?r=gii%2Fdefault%2Fview&id=model ซึ่งยากที่ user ทั่วไปจะจำ ทำให้ google มองว่าเว็บเราใช้งานยากจึงไม่เป็นผลดีในการทำ seo เราจึงต้องแปลง query string ให้เป็นรูปแบบ pretty urls ที่จะจำได้ง่ายและสวยงามกว่า พี่เกิ๊ลชอบ

  1. เปิดไฟล์ config\main.php จากนั้นเพิ่ม[code language=”php” title=”common\config\main.php”] ‘components’ => [

    ‘urlManager’ => [
    ‘enablePrettyUrl’ => true,
    ‘showScriptName’ => false,
    ],

    ],[/code]
  2. จากนั้นสร้างไฟล์[code language=”text” title=”.htaccess”]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[/code]save ไปที่ backend\web และ frontend\web

เท่านี้จาก url ยาวๆ ก็จะสวยงามขึ้นกลายเป็น http://localhost/BanBan/1/backend/web/gii/model อ่านง่ายขึ้นเยอะเลย

About the author

phunsanit administrator

Leave a Reply