为什么我的.htaccess文件重定向到完整的服务器路径,而不是相对路径?我的、路径、而不是、重定向

2023-09-02 00:37:01 作者:离线请留言

我从来没有使用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的使用了mod_rewrite的.htaccess文件将请求重定向到自己的根目录文件夹中。问题是,重定向列出了错误的道路,并导致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://主机名。 COM /〜用户名/目录/程序/ Web根目录/ 然而,而不是简单地增加应用程序/ Web根目录/ 到最后,因为它应该是,它增加了完整的服务器路径( /家/用户名/的public_html /目录/程序/ Web根目录/ ),导致最终的网址 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.

推荐答案

解决您的问题都可以向这条底线的页在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语句添加到.htaccess文件的CakePHP用途(/.htaccess,/app/.htaccess,/app/webroot/.htaccess)。

For many hosting services (GoDaddy, 1and1), your web server is actually being served from a user directory that already uses mod_rewrite. If you are installing CakePHP into a user directory (http://example.com/~username/cakephp/), or any other URL structure that already utilizes mod_rewrite, you'll need to add RewriteBase statements to the .htaccess files CakePHP uses (/.htaccess, /app/.htaccess, /app/webroot/.htaccess).

我已经部署的CakePHP从我的个人资料的的public_html文件夹中。我不得不改变3上述相同的.htaccess文件。只需添加的RewriteBase /〜用户名/ 来的.htaccess文件刚过 RewriteEngine叙述

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!