URL 格式:https://serverName/appName/module/action/param1/value1/param2/value2/
通過 Web 服務器重寫模塊將請求內部轉發到 `index.php`,實現更優雅的 URL。
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
# 或更標準的 try_files
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php($|/) {
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
'url_model' => 2, // 1=普通, 2=pathinfo, 3=rewrite, 4=兼容
'url_html_suffix' => 'html', // 可選偽靜態後綴
'url_route_on' => true, // 開啟路由
| 模式 | URL 示例 | 優點 | 缺點 |
|---|---|---|---|
| 普通 (0/1) | ?m=Home&c=Index&a=index | 兼容性最好,無需服務器配置 | 不美觀、SEO 弱 |
| Pathinfo (2) | /Home/Index/index/id/1 | 語義清晰、SEO 友好、無需重寫 | URL 中含 index.php(未重寫時) |
| Rewrite (3) | /Home/Index/index/id/1 | 最美觀、SEO 最佳、隱藏入口 | 需服務器支持重寫模塊 |