0
0

More than 3 years have passed since last update.

CodeIgniterのルーティング設定

Posted at

前提条件

ルートURLにアクセスすると
CodeIgniterのwelcomeページが表示される
(例)http://example.com/

スクリーンショット 2020-07-03 11.33.20.png

課題

application/config/routes.phpの設定で、

$route['home']='home/index';

というルーティングを設定しても
/index.php/home
にアクセスしなければindexメソッドが実行されない

これを/homeで実行できるようにしたい。

apacheのmod_rewriteモジュールを使う

mod_rewriteでは、アクセスURLを別のURLに変換してアクセスさせる

つまり、/exampleというアクセスを/index.php/exampleに変換してアクセスさせるように
設定すると上記の課題は解決できる。

流れとしては、mod_rewriteモジュールを有効にし、
.htaccessでmod_rewriteの設定をapacheの設定に加える

#apacheのmod_rewriteモジュールを読み込む
$ a2enmod rewrite
#apacheの設定にて、公開ディレクトリでの.htaccessを有効にする
$ vi /etc/apache2/apache2.conf
apache2.conf
#加える設定
<Directory /var/www/html/>
        Options Indexes FollowSymLinks
        #↓これ
        AllowOverride All
        Require all granted
</Directory>

その後公開ディレクトリ(/var/www/html)配下に、下記の内容の.htaccessを作成

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

設定が完了したら、apacheをリスタート

$ service apache2 restart

これでルーティングは正常に機能する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0