情シス - 雑多メモ

基本は情シス、たまに開発を担当

Codeigniterのindex.php

Codeigniterの初期状態は、URLに「index.php」を含む設定になっています。

環境:Codeigniter-3.1.4

URL例)

example.com/index.php/news/article/my_article


[参考]Codeigniter公式
CodeIgniter の URL — CodeIgniter 3.2.0-dev ドキュメント


index.phpと同じ階層に.htaccessを設置し、除外ルールを記述することでURLから「index.php」を省略してアクセスすることができます。
URLから「index.php」を取り除きたい場合は以下の通りです。


Apacheの設定

.htaccessの読み込みが有効になっていることを確認
AllowOverride  「None」の場合は「All」に変更してApacheを再起動する

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

.htaccessの編集

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

config.phpの編集

application/config/config.phpの[index_page]を''に変更

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';