I'm working on an cake app which is inside a sub directory (because I need to run it along side another app on the same domain). I want the pages powered by cake to appear as if they'r e top level.
So instead of example.com/cake_dir/page...
I want example.com/page...
I think the best way to make this work is by adding a rewrite rule to an .htaccess file, so here's what I want to know:
- Is this the best way to do this? If so:
- What is the proper syntax for that kind of rule?
- Where should that file go? The domain root? In the cake_dir? Or in cake's webroot?
- If one of Cake's .htaccess files already exists in that location, should the new rule go before or after and should and of the
[L]
s be removed?
UPDATE: Clearer description of what I'm trying to do
example.com/ <--root
/_cake <--CakePHP installed here
/page3
/page4
Currently, visitors have to access page 3 by going to example.com/_cake/page3. This sucks. Instead I want this: example.com/page3 and if cake routes to example.com/_cake/page4 I want it to appear as example.com/page4 in the browser.
I've been cramming as much mod_rewrite knowledge as possible and I think the way to do this is to do an internal rewrote to show the correct content when the _cake is absent, and an external rewrite for when _cake IS present.
Update Here are the existing .htaccess files...
.htaccess in /_cake...
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
.htaccess in /_cake/app...
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
.htaccess in /_cake/app/webroot...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ index.php #[QSA,L]
RewriteRule ^(.*)$ index.php [QSA]
</IfModule>
Update 2 I added anubhava's code to the root .htaccess file, and then also updated the .htaccess file at /_cake/ to look like the following (notice that I had to remove the [L]s in order for CakePHP's normal rewrites to still work):
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /_cake/page to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+_cake/([^\s]+) [NC]
RewriteRule ^ /%1 [R=302]
# internal forward from /page to /_cake/page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^_cake/)^(.*)$ /_cake/$1 [NC]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>