CakePHP in a subdirectory and no trailing slash
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
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
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/
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
Question
1 Losing path to webroot items
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...
Comment
2 Method update
http://groups.google.com/group/cake-php/browse_thread/thread/a6bcbad3a9bc7ed3/f7909fd494417a08?lnk=gst&q=bad+request&rnum=1#f7909fd494417a08
Comment
3 Another solution...
Question
4 Failed installation...
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
::