スポンサーリンク
業務でCakePHPの動作させる環境を構築する際、わけあって .htaccess のファイルを使わずに構築する事態に陥りました。通常、CakePHPではApacheのmod_rewriteモジュールを使用させてURLを生成していますが、mod_rewriteを使わずにCakePHPを動作させる方法を紹介します。
以下の話は、2015年1月12日時点での安定版、CakePHP 2.5.8を想定しています。
方法はとても簡単です。
まず、app/Config/core.php をエディタで開きます。
中身の107行目付近に
/**
* To configure CakePHP *not* to use mod_rewrite and to
* use CakePHP pretty URLs, remove these .htaccess
* files:
*
* /.htaccess
* /app/.htaccess
* /app/webroot/.htaccess
*
* And uncomment the App.baseUrl below. But keep in mind
* that plugin assets such as images, CSS and JavaScript files
* will not work without URL rewriting!
* To work around this issue you should either symlink or copy
* the plugin assets into you app's webroot directory. This is
* recommended even when you are using mod_rewrite. Handling static
* assets through the Dispatcher is incredibly inefficient and
* included primarily as a development convenience - and
* thus not recommended for production applications.
*/
//Configure::write('App.baseUrl', env('SCRIPT_NAME'));
/**
という箇所があるので、「Configure::write(‘App.baseUrl’, env(‘SCRIPT_NAME’));」の箇所をコメントを取り除きます。
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
その後、CakePHPの各ディレクトリに存在する .htaccess ファイルを削除します。
■削除対象
/.htaccess
/app/.htaccess
/app/webroot/.htaccess
上記の3つの.htaccessを削除してください。
これで、mod_rewriteを使わずにCakePHPのシステムを動作させることができます。お試しあれ。
★参考
CakePHP Configuration
http://book.cakephp.org/2.0/en/development/configuration.html
スポンサーリンク
スポンサーリンク