萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

CodeIgniter 移除 URL 中 index.php 完整配置指南 (Apache + Nginx)

CodeIgniter (CI) 預設 URL 包含 `index.php` (如 `example.com/index.php/controller/method`),影響美觀與 SEO。本文提供 Apache `.htaccess` 與 Nginx `rewrite` 兩套完整配置,並說明 CI 內部配置修改與常見坑點。

步驟 1:修改 CI 配置

檔案:`application/config/config.php`

$config['index_page'] = '';  // 預設 'index.php',改為空字串
$config['uri_protocol'] = 'REQUEST_URI'; // 推薦:AUTO/REQUEST_URI/PATH_INFO/QUERY_STRING

步驟 2:Apache 配置 (.htaccess)

位置:CI 專案根目錄 (與 `index.php` 同層)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

# 若專案在子目錄 (如 /ci/),需加入 RewriteBase
# RewriteBase /ci/

關鍵指令解析

  • `RewriteCond %{REQUEST_FILENAME} !-f`:請求非實體檔案
  • `RewriteCond %{REQUEST_FILENAME} !-d`:請求非實體目錄
  • `RewriteRule ^(.*)$ index.php/$1 [L,QSA]`:重寫至 `index.php` 保留路徑,`QSA` 附加查詢字串
  • 確保 `httpd.conf` 載入 `mod_rewrite` 並允許 `AllowOverride All`

步驟 3:Nginx 配置

server {
    listen 80;
    server_name example.com;
    root /var/www/ci/public;  # 指向 public 目錄 (若有) 或專案根目錄
    index index.php index.html;

    location / {
        # 核心:嘗試檔案 → 目錄 → 重寫到 index.php
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock; # 或 127.0.0.1:9000
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param PATH_INFO $fastcgi_path_info; # CI 需 PATH_INFO
    }

    # 安全:隱藏隱藏檔、版本控制目錄
    location ~ /\.(git|svn|env|htaccess) { deny all; }
}

子目錄部署特殊處理

若 CI 安裝在 `example.com/ci/`:

Apache (.htaccess)

RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Nginx

location /ci/ {
    alias /var/www/ci/;  # 實際專案路徑
    try_files $uri $uri/ /ci/index.php?$query_string;

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename; # alias 下用 $request_filename
        include fastcgi_params;
    }
}

靜態資源排除 (CSS/JS/Images 不走 index.php)

Apache:`RewriteCond` 已自動排除實體檔案 (`!-f`)。

Nginx:`try_files $uri $uri/ ...` 會優先匹配實體檔案。

若仍有問題,可顯式排除:

# Apache
RewriteCond $1 !^(index\.php|assets|uploads|robots\.txt|favicon\.ico)

# Nginx
location ~* \.(css|js|jpg|png|gif|ico|woff|woff2)$ {
    expires 1y; add_header Cache-Control "public";
    try_files $uri =404; # 不轉發 PHP
}

驗證與除錯

  1. 訪問 `https://example.com/controller/method` 應正常顯示 (無 `index.php`)
  2. 檢查 `$_SERVER['REQUEST_URI']`、`$_SERVER['PATH_INFO']` 是否正確
  3. 啟用 CI 日誌:`$config['log_threshold'] = 4;` 查看 `application/logs/`
  4. 常見 404 原因:`mod_rewrite` 未啟用、`AllowOverride None`、`.htaccess` 權限錯誤、Nginx `try_files` 順序錯誤

CI 3.x vs 4.x 差異

項目CI 3.xCI 4.x
配置檔`application/config/config.php``app/Config/App.php` (`$indexPage = ''`)
核心入口`index.php``public/index.php`
URI 協定`$config['uri_protocol']`自動偵測,可在 `App.php` 設定 `$uriProtocol`
重導向`redirect()``return redirect()->to(...)`

正確移除 `index.php` 能讓 CI 專案擁有乾淨的 RESTful 風格 URL,提升 SEO 與使用者體驗。Apache 用 `.htaccess`,Nginx 用 `try_files`,核心邏輯一致:實體檔案優先,其餘轉發 index.php 處理路由。

copyright © 萬盛學電腦網 all rights reserved