CakePHP in a subdirectory; no trailing slash
Posted by curt on November 2nd, 2006I recently worked on a project using the CakePHP web framework. One problem I had was placing the framework in a subdirectory of the main website and accessing the application without a trailing slash:
This worked:
http://mysite.com/sub/
This did not:
http://mysite.com/sub
Usually Apache figures out that "sub" is a directory and will redirect to the slashed location for you. However, CakePHP's default mod_rewrite rules play havoc with this and the result was a 400 Bad Request error. The solution is relatively simple, but was not intuitively easy to figure out.
CakePHP ships with this .htaccess file in the base CakePHP directory:
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ app/webroot/ [L] RewriteRule (.*) app/webroot/$1 [L] </IfModule>
In my attempts to solve this problem, I had tried several rewrite rules and redirects from within this file and from within my website's root web directory (one above the CakePHP directory), but only the following worked for me:
1.) Put this in your website root directory (where "sub" is your CakePHP directory):
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^sub$ sub/app/webroot/ [L] RewriteRule ^sub/(.*)$ sub/app/webroot/$1 [L] </IfModule>
2) AND, DELETE OR RENAME your .htaccess file in the CakePHP sub directory.
Hope that helps someone.
Recent Comments