預設 Yii URL 格式 ?r=controller/action 不利 SEO,本文示範四步驟實現 /controller/action.html 友善路徑。
# httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so # 取消註解
<Directory "D:/var/www/html">
AllowOverride All # 確保存在
</Directory>
重啟 Apache。
編輯 protected/config/main.php:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'urlSuffix'=>'.html',
'rules'=>array(),
),
urlFormat='path':改用 /controller/action/param/value 格式showScriptName=false:隱藏 index.phpurlSuffix='.html':偽靜態後綴Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
原 ?r=admin/UserInfo/admin 現可用 /admin/UserInfo/admin.html 存取。
AllowOverride All、mod_rewrite 載入、.htaccess 權限RewriteBase 是否需設定rules 需進一步定義參數對應規則