【CI4 + XServer】XServerでCodeIgniter4を特殊なディレクトリ構造で設置する方法

XServerにCodeIgniter4アプリケーションをアップロードした際、トップページにはアクセスできるものの、下層ページにアクセスすると「No input file specified.」というエラーが表示される問 […]

XServerにCodeIgniter4アプリケーションをアップロードした際、トップページにはアクセスできるものの、下層ページにアクセスすると「No input file specified.」というエラーが表示される問題が発生しました。

ディレクトリ構造

xxxxx.com/
├── public_html/
│   └── xxx.xxxxx.com/        # publicフォルダの中身
│       ├── index.php
│       ├── .htaccess
│       └── assets/
└── apps/
    └── xxx.xxxxx.com_app/    # その他のCodeIgniterファイル
        ├── app/
        ├── system/
        ├── writable/
        └── .env ...

セキュリティ向上のため、公開フォルダとアプリケーションフォルダを分離した構成です。

原因

XServerでは、.htaccessのRewriteRuleでindex.php/$1という形式を使用すると正しく動作せず、「No input file specified.」エラーが発生します。これはXServerのPHP-CGI設定に起因する問題です。

解決方法

1. .htaccessファイルの修正

public_html/wac.momentalpha.jp/.htaccessを以下のように修正します:

# Disable directory browsing
Options -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    
    # RewriteBaseのコメントアウトを解除(重要)
    RewriteBase /
    
    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
    
    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
    
    # Front Controllerへのルーティング
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
    
    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>

# Disable server signature
ServerSignature Off

重要な変更点

  1. RewriteBase /を有効化
    • コメントアウトを解除することで、正しいベースパスが設定されます
  2. index.php/$1index.php?/$1に変更
    • XServerではクエリ文字列形式(?/)が必要です
    • これがこの問題の最重要ポイントです
  3. 不要なフラグを削除
    • [L,NC,QSA][L,QSA]に変更
    • NCフラグは不要なため削除

まとめ

XServerでCodeIgniter4を使用する際の重要なポイント:

  • .htaccessRewriteBase /を必ず有効化する
  • RewriteRuleはindex.php?/$1形式を使用する(index.php/$1ではない)

この設定により、セキュアなディレクトリ構造を維持しながら、XServer上でCodeIgniter4を正常に動作させることができます。