CakePHP in a subdirectory and no trailing slash

By Curtis Summers aka "curtis.summers"
Are you putting CakePHP in a subdirectory and experiencing problems with no trailing slash and 400 Bad Request errors?
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:

Download code
<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):
Download code
<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.

From this blog post:
http://www.csummers.org/index.php/2006/11/02/cakephp-in-a-subdirectory-no-trailing-slash/

Comments 132

CakePHP team comments Author comments

Question

1 Losing path to webroot items

Hey Curtis,

I gave this a shot and while the redirection worked great, I lost the proper links to the CSS, JavaScript and Image directories (set in the default layout). Using your example, using the Cake helpers caused code to be created without the 'sub'.

Did I miss something? I'd love to get this working :)

All the best...
posted Tue, Nov 28th 2006, 04:12 by Othman ouahbi

Comment

2 Method update

I tried this to no effect on one of my larger sites (with a cake app in a sub-dir). I found this related thread on the CakePHP Google group which worked for me fine.

http://groups.google.com/group/cake-php/browse_thread/thread/a6bcbad3a9bc7ed3/f7909fd494417a08?lnk=gst&q=bad+request&rnum=1#f7909fd494417a08
posted Wed, Jan 3rd 2007, 09:25 by Mark Thompson

Comment

3 Another solution...

posted Thu, Apr 19th 2007, 09:17 by Jeff Loiselle

Question

4 Failed installation...

I just uploaded v 1.0.0.2 to a sub directory and received this error:

Warning: beforefilter(/home/dream/public_html/shop/app/config/bakesale_config.php) [function.beforefilter]: failed to open stream: No such file or directory in /home/dream/public_html/shop/app/app_controller.php on line 26

Fatal error: beforefilter() [function.require]: Failed opening required '/home/dream/public_html/shop/app/config/bakesale_config.php' (include_path='/home/dream/public_html/shop:/home/dream/public_html/shop/app/:.:/usr/lib/php:/usr/local/lib/php') in /home/dream/public_html/shop/app/app_controller.php on line 26

Can anyone please offer assistance.

Frank

::
posted Tue, Dec 4th 2007, 08:06 by Frank Newhaven

Login to Submit a Comment