CakePHP in a subdirectory; no trailing slash
programming 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.
November 6th, 2006 at 3:03 am
Great ! It works !
Thanx !
December 2nd, 2006 at 12:12 am
Thanks! Works for me too.
January 8th, 2007 at 2:52 pm
I'm trying this out on a local dev copy that uses MAMP, and I wanted to check a couple of things to make sure I'm trying exactly what you suggest.
In my case "sub" is "admin" so I have a site at:
http://localhost:8888/
And I'm trying to add a cake app in
http://localhost:8888/admin/
This trick works great for the index page, but dies on any other page. So, just to be sure:
*I put your .htaccess file in http://localhost:8888/
*I delete the .htaccess in http://localhost:8888/admin/
*What about the .htaccess file in admin/app? (I'm assuming I shouldn't touch the one in admin/app/webroot.)
There may be some craziness with MAMP, but I wanted to make sure I wasn't screwing up these directions.
January 8th, 2007 at 2:57 pm
You might try removing all subsequent .htaccess in further-down directories. Any additional .htaccess that rewrites paths would interfere with your one in http://localhost:8888/
Let me know how it works out.
January 15th, 2007 at 6:59 pm
That worked out smashingly on the local copy, but now I tried it out on our server and it starts throwing 403 errors for anything on the site (even pages that aren't in the admin directory). Any ideas on what might be causing the problem?
January 15th, 2007 at 7:15 pm
403 Errors are usually related to your file permissions and/or your Apache configuration. Based on this Wordpress post about mod_rewrite and 403's, I suggest you try adding Options +FollowSymlinks to your .htaccess or to your virtual host config in httpd.conf.
February 5th, 2007 at 8:05 am
This works for me but I lose all my CSS. Any ideas why?
April 19th, 2007 at 9:18 am
I wrote a different hack for this as well. http://jeff.loiselles.com/wordpress/?p=22