问题描述
我以前从未遇到过 cakePHP 问题,但是这个服务器有些奇怪,导致 .htaccess 文件中的重定向行为异常.
I've never had a problem with cakePHP before, but something's odd about this server and is causing the redirects in the .htaccess files to behave oddly.
CakePHP 在 .htaccess 文件中使用 mod_rewrite 将请求重定向到它自己的 webroot 文件夹.问题是重定向列出了错误的路径并导致 404 错误.我的 CakePHP 应用程序存储在列表目录中,有一个 .htaccess 文件,如下所示:
CakePHP uses mod_rewrite in .htaccess files to redirect requests to its own webroot folder. The problem is that the redirects are listing the wrong path and causing a 404 error. My CakePHP application, which is stored in the listings directory, has a .htaccess file as follows:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [R=301,L]
RewriteRule (.*) app/webroot/$1 [R=301,L]
</IfModule>
(*注意,R=301 会导致外部重定向,因此我们可以看到我们到底发生了什么.它应该真正省略此标志并在内部进行重定向,对最终用户透明)
(*note that the R=301 causes an external redirect so we can see what is going on from our end. It should really omit this flag and do the redirect internally, transparent to end-users)
这应该将任何请求从 http://hostname.com/~username/listings/
重定向到 http://hostname.com/~username/listings/app/webroot/
然而,它不是简单地在末尾添加app/webroot/
",而是添加完整的服务器路径(/home/username/public_html/listings/app/webroot/
) 导致最终 URL http://hostname.com/home/username/public_html/listings/app/webroot/
这显然不正确并触发 404 错误.
This is supposed to redirect any request from http://hostname.com/~username/listings/
to http://hostname.com/~username/listings/app/webroot/
However, rather than simply adding "app/webroot/
" to the end as it is supposed to, it is adding the full server path ( /home/username/public_html/listings/app/webroot/
) resulting in the final URL http://hostname.com/home/username/public_html/listings/app/webroot/
which is obviously incorrect and triggers a 404 error.
托管在共享托管帐户上,因此限制了我可以对设置执行的操作.我以前从未见过这种情况,我认为这是托管方面的问题,但如果有人有一些有用的建议,那么我也可以将它们提交给托管公司.
The hosting is on a shared hosting account, so that limits what I can do with the settings. I've never seen this happen before, and I'm thinking it's something wrong from the hosting side of things, but if anyone has some helpful suggestions then I can put them to the hosting company as well.
推荐答案
您的问题的解决方案可以在此page 在 cakephp 书中:
The solution to your question can be found towards the bottom of this page in the cakephp book:
对于许多托管服务(GoDaddy、1and1),您的 Web 服务器实际上是从已经使用 mod_rewrite 的用户目录提供服务的.如果您将 CakePHP 安装到用户目录 (http://example.com/~username/cakephp/) 或任何其他已经使用 mod_rewrite 的 URL 结构,您需要将 RewriteBase 语句添加到 CakePHP 使用的 .htaccess 文件中(/.htaccess、/app/.htaccess、/app/webroot/.htaccess).
我也从我的个人资料的 public_html 文件夹中部署了 CakePHP.我不得不更改上面提到的 3 个相同的 .htaccess 文件.只需在 RewriteEngine on
之后将 RewriteBase/~username/
添加到 .htaccess 文件中即可!
I've deployed CakePHP from my profile's public_html folder as well. I had to change 3 the same .htaccess files mentioned above. Just add RewriteBase /~username/
to the .htaccess files just after RewriteEngine on
!
这篇关于为什么我的 .htaccess 文件重定向到完整的服务器路径而不是相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!