apache - 如何使子目录中的 cakePHP 2.x 出现在根目录中(在 htaccess 中使用 mod_rewrite)?

标签 apache .htaccess cakephp mod-rewrite cakephp-2.0

<分区>

我正在开发一个位于子目录内的蛋糕应用程序(因为我需要将它与同一域中的另一个应用程序一起运行)。我希望由 cake 支持的页面看起来像是顶级页面。

所以不是 example.com/cake_dir/page... 我想要 example.com/page...

我认为完成这项工作的最佳方法是向 .htaccess 文件添加重写规则,所以我想知道的是:

  1. 这是最好的方法吗? 如果是这样:
  2. 这种规则的正确语法是什么?
  3. 该文件应该放在哪里?域根?在 cake_dir 中?或者在 cake 的 webroot 中?
  4. 如果 Cake 的 .htaccess 文件之一已经存在于该位置,新规则应该在之前还是之后,并且是否应该删除 [L]

更新:更清楚地描述我正在尝试做的事情

example.com/ <--root
    /_cake   <--CakePHP installed here
        /page3
        /page4

目前,访客必须前往 example.com/_cake/page3 才能访问第 3 页。这很糟糕。相反,我想要这个:example.com/page3 如果蛋糕路由到 example.com/_cake/page4 我希望它在浏览器中显示为 example.com/page4。

我一直在尽可能多地灌输 mod_rewrite 知识,我认为这样做的方法是在 _cake 不存在时进行内部重写以显示正确的内容,并在 _cake 存在时进行外部重写。

更新 这是现有的 .htaccess 文件...

.htaccess 在/_cake...

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

.htaccess 在/_cake/app...

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

.htaccess 在/_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>

更新 2 我将 anubhava 的代码添加到根 .htaccess 文件中,然后还更新了位于/_cake/的 .htaccess 文件,如下所示(请注意,我必须删除 [L] 才能使 CakePHP 的正常重写仍然有效) :

# 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>

最佳答案

$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]

关于apache - 如何使子目录中的 cakePHP 2.x 出现在根目录中(在 htaccess 中使用 mod_rewrite)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15869240/

相关文章:

c++ - Apache 权限被拒绝连接到套接字

django - 无法使用 Apache 和 mod_wsgi 使简单的 Django Web 应用程序正常工作

javascript - 如果在 angularjs 中启用了 html5mode,如何重新加载页面?

wordpress - 将一张图片重定向到网页问题

.htaccess - 使用 htaccess 强制 SSL 和 WWW 一起使用

mysql - CakePHP 保存默认 0 值

apache - 请求 header 与响应 header

apache - CF10 mod_jk.so 无法使用 RHEL 5.6 和 Apache httpd 2.2.3 加载

sqlite - SQLite:如何将默认输入类型设置为 `text`而不是 `textarea`

cakephp - 在 Controller 中添加 HABTM 数据