How to make cakePHP 2.x in sub directory appear in root (with mod_rewrite in htaccess)? [closed]

百般思念 提交于 2019-12-06 05:41:15

$DOCUMENT_ROOT/.htaccess:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# internal forward from /page to /_cake/page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^_cake/)^(.*)$ /_cake/$1 [L,NC]

$DOCUMENT_ROOT/_cake/.htaccess:

RewriteEngine on
RewriteBase /_cake/
RewriteRule (.*) app/webroot/$1 [L]

$DOCUMENT_ROOT/_cake/app/.htaccess:

RewriteEngine on
RewriteBase /_cake/app/
RewriteRule (.*) webroot/$1 [L]

$DOCUMENT_ROOT/_cake/app/webroot/.htaccess:

RewriteEngine On
RewriteBase /_cake/app/webroot/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+_cake/([^\s&]*) [NC]
RewriteRule ^ /%1 [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

If you want to make it so when someone requests http://example.com/page they actually get served the content provided by /cake_dir/page, then you should add these rules to the htaccess file in your document root (or "domain root"):

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/cake_dir%{REQUEST_URI} -f
RewriteRule ^(.*)$ /cake_dir/$1 [L]

The URL in the browser's address bar will remain unchanged because there is no external redirect. The condition here is that it checks if the %{DOCUMENT_ROOT}/cake_dir%{REQUEST_URI} points to a file. That string essentially puts together the document root, then the string "cake_dir", then the actual requested file. In the case that none of these requests actually point to physical files in the "cake_dir", you can try:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cake_dir/$1 [L]

Here, the conditions are that the requested URI (e.g. /page) does not point to an existing file or directory. And it routes everything blindly into the /cake_dir directory.


To redirect the browser in the event that someone clicks on a link with cake_dir in it, then you need these rules:

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /cake_dir/
RewriteRule ^cake_dir/(.*)$ /$1 [L,R=301]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!