【问题标题】:CakePHP htaccess redirecting to live site when running site locally在本地运行站点时,CakePHP htaccess 重定向到实时站点
【发布时间】:2012-05-31 17:29:11
【问题描述】:

我正在开发一个使用 CakePHP 的网站。我需要开始向该站点添加一些功能,因此我将所有内容从实时站点拉到我的开发机器(带有 MAMP 服务器的 mac os lion)。当我尝试加载站点 (localhost/example.com/) 时,它会自动将我重定向到实时站点 (www.example.com)。

这是根 htaccess 文件的内容:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{HTTP_HOST}   !^$
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

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

当我注释掉 前两个 RewriteCondfirst RewriteRule 时,我的机器抛出 500 内部服务器错误,没有 apache/php 日志输出。

如何在不自动重定向到实时站点的情况下在本地运行此网站?

我可以提供任何其他信息吗?

编辑:这是我的文档根目录:/Applications/MAMP/htdocs。不应该是/Applications/MAMP/htdocs/example.com吗? 编辑:这是/webroot/.htaccess 文件:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule img/([0-9]*x[0-9]*)/(.*)/(.*)$ files/image/$2/$1/$3 [QSA,L]
    RewriteRule (js|css)/vendors/(.*)$ vendors.php?type=$1&file=$2 [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

AddType application/x-javascript .js
AddType text/css .css

<IfModule mod_deflate.c>
  # compress content with type html, text, and css
  AddOutputFilterByType DEFLATE text/css text/javascript application/javascript application/x-javascript text/js

  SetOutputFilter DEFLATE
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

  # Don't compress images/flash 
  SetEnvIfNoCase Request_URI \
  \.(?:swf|flv)$ no-gzip dont-vary

  # Don't compress images
  # SetEnvIfNoCase          Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  # SetEnvIfNoCase          Request_URI \/image\.php no-gzip dont-vary

  # Also don't compress PDF and Flash-files 17-01-2004 MM
  SetEnvIfNoCase          Request_URI \.pdf$ no-gzip dont-vary

  <IfModule mod_headers.c>
    # properly handle requests coming from behind proxies
    Header append Vary User-Agent
  </IfModule>
</IfModule>

<IfModule mod_expires.c> 
  ExpiresActive On
  ExpiresByType text/css "access plus 10 years"
  ExpiresByType text/js "access plus 10 years"
  ExpiresByType text/javascript "access plus 10 years"
  ExpiresByType application/x-javascript "access plus 10 years"
  ExpiresByType application/javascript "access plus 10 years"
  ExpiresByType image/png "access plus 10 years"
  ExpiresByType image/gif "access plus 10 years"
  ExpiresByType image/jpeg "access plus 10 years"
  ExpiresByType video/x-flv "access plus 10 years"
</IfModule>

FileETag none
<IfModule mod_headers.c>
# 1 YEAR
<FilesMatch "\.(ico|pdf|flv)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>
# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 2 DAYS
<FilesMatch "\.(xml|txt|css|js)$">
Header set Cache-Control "max-age=172800, proxy-revalidate"
</FilesMatch>
# 1 MIN
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>
</IfModule>

编辑:我已经为 apache 启用了调试日志级别,所以我可以看到更多的日志消息。这是访问项目根目录时出现错误 500 时的日志详细信息:

[Fri Jun 01 11:00:35 2012] [debug] mod_deflate.c(615): [client ::1] Zlib: Compressed 0 to 2 : URL /example.com/webroot/index.php

[Fri Jun 01 11:00:35 2012] [debug] mod_headers.c(756): headers: ap_headers_output_filter()

在访问错误 500 的站点后,我还包含了 access_log 数据:

127.0.0.1 - - [01/Jun/2012:11:03:02 -0400] "GET /example.com/ HTTP/1.1" 500 20 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.53 Safari/536.5"

【问题讨论】:

  • 验证apache重写模块是否启用?
  • 我确信重写模块已启用。 (phpinfo() 显示加载的模块,包括 mod_rewrite)。我还使用重写在本地运行其他网站,效果很好(基于 zend 框架)

标签: .htaccess cakephp


【解决方案1】:

用这个替换你现有的 .htaccess 代码:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /example.com/

    RewriteRule (?!^webroot/)^(.*)$ webroot/$1 [L]
</IfModule>

【讨论】:

  • 用你的代码替换我的代码后,我得到这个错误:The requested URL /webroot/ was not found on this server.我的项目定义。包含webroot 目录,它位于我项目的根目录。
  • 只是为了测试,将上面的 .htaccess 重命名为其他内容,然后尝试加载 http://site.com/webroot/ 它是否打开?您是否还有另一个 .htaccess 在 webroot 中?
  • 也想知道你的$DOCUMENT_ROOT是什么?
  • webroot 中有一个 htaccess。我已经更新了我的问题以包含更多信息。你也提到要去http://site.com/webroot/。你的意思是让我访问现场?执行此操作后(在实时站点上),页面正常加载,但显示页面未找到消息。访问 localhost/site.com/webroot 导致 500 内部服务器错误
  • 感谢您的更新,您能否也提供您的 exact 目录结构,如以下问题:stackoverflow.com/questions/10839562/…。我很难理解 webroot 文件夹的实际位置。是否直接在 $DOCUMENT_ROOT 下。
猜你喜欢
  • 2015-08-06
  • 2017-09-13
  • 1970-01-01
  • 2017-09-03
  • 2018-01-06
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多