【问题标题】:Defining Document Root in Apache2/Cake PhP在 Apache2/Cake PhP 中定义文档根目录
【发布时间】:2014-03-20 03:44:22
【问题描述】:

我有一个损坏的 Cake PHP 站点,不是我编写的,而是负责修复。我有主控制器,但是没有 css/js/images,当我单击一个链接时,我得到一个 404 未找到。我认为 mod_rewrite 或 apache 中的 docroot 配置有问题。

在阅读 Cake 文档时,我发现了这个:

"应用程序/webroot 在生产设置中,此文件夹应作为应用程序的文档根目录。此处的文件夹还用作存放 CSS 样式表、图像和 JavaScript 文件的地方。”

在我的 /etc/apache2/sites-enabled/this-site 中,我看到了这个:

DocumentRoot /u02/data/docroots/this_site
    <Directory /u02/data/docroots/this_site>
            Options -Indexes
            AllowOverride none
            Order deny,allow
            Allow from all
    </Directory> 

所以,我的问题: 上面的配置块是否需要: /u02/data/docroots/this_site/app/webroot 作为 DocumentRoot 和 ?​​p>

您还可以在哪里寻找解决此问题的方法?

【问题讨论】:

  • 首先,mod_rewrite 是否在 apache 上启用?另外,尝试将AllowOverride 更改为all
  • 你肯定需要AllowOverride none 更改为AllowOverride All @Eagle 提到的。只有在这种情况下,cakephp 使用的 .htaccess 才能工作
  • mod_rewrite 确实启用了。

标签: php apache cakephp mod-rewrite


【解决方案1】:

由于您有权编辑 apache 配置文件 - 最适合进行标准生产安装。

修复文档根

上述配置块是否需要:.../app/webroot 作为 DocumentRoot

假设路径存在:是的。

这将修复损坏的 css/js/images。

修复模组重写

将重写规则放在虚拟主机配置中:

DocumentRoot /u02/data/docroots/this_site/app/webroot
<Directory /u02/data/docroots/this_site/app/webroot>
        Options -Indexes
        AllowOverride none
        Order deny,allow
        Allow from all

        // added
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
</Directory> 

然后,这会将针对不是文件的任何内容的请求路由到应用程序。

验证使用的重写规则是否与使用的CakePHP版本兼容,以上是基于the latest release - it changed over the years的规则,使用错误的重写规则可能会产生意想不到的副作用。

实际损坏了什么

AllowOverride none

这可以防止 apache 读取 .htaccess 文件。如果您将其更改为 AllowOverride all,并假设存在当前被忽略的 .htaccess 文件,则该站点可以正常工作 - 但作为开发安装。

【讨论】:

  • 这些答案非常有帮助。一个后续问题:CakePHP 使用三个 htaccess 文件。它们目前是隐藏的,“.htaccess”。为了让蛋糕使用它们,它们不需要隐藏起来吗?
  • 我之前的评论是我缺乏理解,现在无关紧要。但是,我的最后一个问题 - 它是开发安装,因为 docroot 被定义为 /docroots/this_site 而不是 /docroots/this_site/app/webroot?感谢您的帮助。
【解决方案2】:

试试这个:

<VirtualHost *:80>
ServerAdmin xxxxxx@test.ro
ServerName test.ro
ServerAlias www.test.ro
DirectoryIndex index.php
DocumentRoot /u02/data/docroots/this_site
ErrorLog /u02/data/docroots/this_site/error.log
CustomLog /u02/data/docroots/this_site/access.log combined

<Directory "/u02/data/docroots/this_site">
Options -Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

你应该有 3 个 .htaccess 文件:

/u02/data/docroots/this_site/app/webroot/.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

/u02/data/docroots/this_site/app/.htaccess

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

/u02/data/docroots/this_site/.htaccess

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

希望这会有所帮助;)

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 2015-09-05
    • 2023-03-20
    • 2011-03-19
    • 1970-01-01
    • 2015-11-25
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多