如何让CakePHP的2.X的子目录出现在根(与mod_rewrite的htaccess的)?出现在、子目录、CakePHP、mod_rewrite

2023-09-02 00:44:52 作者:星辰尽在你眼底

我工作的一个蛋糕应用程序,它是在一个子目录(因为我需要沿侧运行在同一域中的其他应用程序)。我想搭载蛋糕的页面看起来好像they'rË最高水平。

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

我觉得,使这项工作的最佳方式是通过添加重写规则.htaccess文件,所以这是我想知道的:

这是做到这一点的最好方法是什么? 如果是这样的: 什么是那种规则的正确语法? 在哪里应该该文件去?该域名的根?在cake_dir?或者在蛋糕的根目录? 如果蛋糕的.htaccess文件已经存在于该位置,如果新规则之前或之后去应和的 [L] s内删除

更新:更清晰的描述我想要做

  example.com/<  - 根
    在这里CakePHP安装 - ; / _cake&LT
        / 3页
        / 4页
 

目前,游客必须通过将example.com/_cake/page3~~V进入第3页。太差劲了。相反,我想这一点:example.com/page3如果蛋糕路线example.com/_cake/page4~~V我希望它出现example.com/page4在浏览器

我一直在恶补尽可能多的mod_rewrite知识可能,我认为要做到这一点的方法是做一个内部重写,以显示正确的内容时,_cake不存在,和外部的重写时_cake IS present。

更新 下面是现有的.htaccess文件...

在/ _cake ...

的.htaccess

 < IfModule mod_rewrite.c>
   RewriteEngine叙述上
   重写规则^ $应用程序/ Web根目录/ [L]
   重写规则(。*)的应用程序/ Web根目录/ $ 1 [L]
< / IfModule>
 
Starbound 零基础开始服务器搭建及安装mod指南

在/ _cake /应用程序...

的.htaccess

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上
    重写规则^ $ Webroot公司/ [L]
    重写规则(。*)根目录/ $ 1 [L]
< / IfModule>
 

在/ _cake / APP / Webroot公司...

的.htaccess

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上
    的RewriteCond%{} REQUEST_FILENAME!-d
    的RewriteCond%{} REQUEST_FILENAME!-f
    #RewriteRule ^(。*)$的index.php#[QSA,L]
    重写规则^(。*)$的index.php [QSA]
< / IfModule>
 

更新2 我说anubhava的code根.htaccess文件,然后还更新了.htaccess文件在/ _cake /到如下所示(请注意,我不得不删除[L] S为了CakePHP的正常重写到仍然工作):

 上#开启mod_rewrite的
RewriteEngine叙述上
的RewriteBase /

#外部重定向从/ _cake /页,/页
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,}  S / + _蛋糕/([^  s] +)[NC]
重写规则^ /%1 [R = 302]

#从/页内着/ _cake /页
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则(?!^ _蛋糕/)^(.*)$ / _cake / $ 1 [NC]


< IfModule mod_rewrite.c>
   RewriteEngine叙述上
   重写规则^ $应用程序/ Web根目录/ [L]
   重写规则(。*)的应用程序/ Web根目录/ $ 1 [L]
< / IfModule>
 

解决方案

$ DOCUMENT_ROOT / htaccess的:

 选项+了FollowSymLinks -MultiViews
#开启mod_rewrite的上
RewriteEngine叙述上
的RewriteBase /

#从/页内着/ _cake /页
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则(?!^ _蛋糕/)^(.*)$ / _cake / $ 1 [L,NC]
 

$ DOCUMENT_ROOT / _cake / htaccess的:

  RewriteEngine叙述上
的RewriteBase / _cake /
重写规则(。*)的应用程序/ Web根目录/ $ 1 [L]
 

$ DOCUMENT_ROOT / _cake / APP / htaccess的:

  RewriteEngine叙述上
的RewriteBase / _cake / APP /
重写规则(。*)根目录/ $ 1 [L]
 

$ DOCUMENT_ROOT / _cake / APP / Webroot公司/ htaccess的:

  RewriteEngine叙述上
的RewriteBase / _cake / APP / Webroot公司/

的RewriteCond%{THE_REQUEST} ^ [AZ] {3,}  S / + _蛋糕/([^  S&放大器;] *)[NC]
重写规则^ /%1 [R = 302,L]

的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^的index.php [L]
 

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>

解决方案

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