【问题标题】:.htaccess for cakephpcakephp 的 .htaccess
【发布时间】:2010-11-23 00:20:12
【问题描述】:

我正在尝试让 CakePHP 应用程序工作。为此,我设置了一个全新的 Debian 安装,更新了配置并将所有内容放在 /var/www 中,其中包含以下内容:

app
cake
.htaccess
index.php
vendors

.htaccess 文件包含以下内容:

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

如果我访问我的虚拟主机 (http://myhost/),我会看到正确的页面。但即使是用 src="/js/validate.js" 加载的 JavaScript 也会失败(它位于 /var/www/app/webroot/js/validate.js 内):

[Wed Aug 26 15:45:12 2009] [error] [client 10.7.10.52] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Wed Aug 26 15:45:12 2009] [debug] core.c(3063): [client 10.7.10.52] r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] mod_deflate.c(632): [client 10.7.10.52] Zlib: Compressed 649 to 405 : URL /webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js

因此我的问题:CakePHP 所需的正确.htaccess 是什么?

非常感谢!

【问题讨论】:

  • 您的 apache 配置中的站点根目录是哪个目录?它应该是 /var/www/app/webroot - 这可能会解决您的问题。
  • @inkedmn:谢谢,应该是 /var/www - 但错误是因为我只使用了一个 .htaccess,而不是 3,正如我刚刚发现的那样。谢谢!

标签: php .htaccess cakephp apache2


【解决方案1】:

正确的.htaccess是默认的:

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

你必须在“/etc/apache2/sites-enabled/default”中添加这个:

<Directory /var/www>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  allow from all
</Directory>

如果部分已存在,请将 AllowOverride None 更改为 AllowOverride All

【讨论】:

    【解决方案2】:

    答案是有 3 个不同的 .htaccess 文件:

    /var/www/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>
    

    /var/www/app/.htaccess

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

    /var/www/.htaccess

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

    这是我的错,所有内容都列在 CakePHP site 上。谢谢大家!

    【讨论】:

    • 在找到我的日志文件并谷歌搜索您上面列出的错误后,我发现了这个!非常感谢!
    • 如何在 cakephp3 中使用此方法,因为 cakephp 3 中没有 app 文件夹,请帮助我..
    • 图片未加载,链接未打开,但索引页面正在运行。请帮助@MrG
    【解决方案3】:

    从主文件中删除 .htaccess 可能会解决这个问题。它对我有用(不需要从 webroot 中删除)

    【讨论】:

      【解决方案4】:

      如果您的提供商允许,您可以将所有内容放入 httpd.conf 文件中,如下所示

      <Directory /var/www>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        <IfModule mod_rewrite.c>
            RewriteEngine on
            RewriteRule    ^$    webroot/    [L]
            RewriteRule    (.*) webroot/$1    [L]
        </IfModule>
      </Directory>
      

      对于其他目录等等...

      【讨论】:

        【解决方案5】:

        CakePHP 在 webroot 目录中只有一个 .htaccess 文件。不需要更多 .htaccess 文件。您必须将app/webroot 设置为您的DOCUMENT_ROOT

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-05-04
          • 2014-02-04
          • 1970-01-01
          • 1970-01-01
          • 2015-12-03
          • 2010-11-22
          • 2013-12-15
          相关资源
          最近更新 更多