php 为共享主机设置修复 cakephp .htaccess/mod_rewrite

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/952999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:26:52  来源:igfitidea点击:

fixing cakephp .htaccess/mod_rewrite for shared hosting setups

php.htaccessmod-rewritecakephp

提问by user117643

I'm trying to install cakePHP on a shared hosting setup. After extracting the files to a location like ~/public_html/dev/cakeand visiting the appropriate URL (in this case http://hostname/~username/dev/cake/), I receive a 404 error:

我正在尝试在共享主机设置上安装 cakePHP。将文件解压缩到~/public_html/dev/cake 之类的位置并访问相应的 URL(在本例中为http://hostname/~username/dev/cake/)后,我收到 404 错误:



未找到

The requested URL /usr/home/username/public_html/dev/cake/app/webroot/ was not found on this server.

在此服务器上找不到请求的 URL /usr/home/username/public_html/dev/cake/app/webroot/。



I suspect that the reason for this is that upon closer inspection, the absolute path to ~/public_htmlis not in fact /usr/home/username/public_html, but rather /usr/www/users/username/.

我怀疑这样做的原因是经过仔细检查,~/public_html的绝对路径实际上不是/usr/home/username/public_html,而是/usr/www/users/username/

Here's what I've been trying (but obviously it's not working): (~/public_html/dev/cake/app/webroot/.htaccess)

这是我一直在尝试的(但显然它不起作用):(~/public_html/dev/cake/app/webroot/.htaccess)

<IfModule mod_rewrite.c>  
    RewriteEngine On  
    RewriteBase /usr/www/users/username/dev/cake/app/webroot/ 
    RewriteCond %{REQUEST_FILENAME} !-d  
    RewriteCond %{REQUEST_FILENAME} !-f  
    RewriteRule ^(.*)$ index.php?url= [QSA,L]  
</IfModule>  

Unfortunately, this doesn't seem to change anything (the 404 message remains that same). Thoughts?

不幸的是,这似乎没有改变任何东西(404 消息保持不变)。想法?

回答by artlung

Shouldn't

不应该

RewriteBase /usr/www/users/username/dev/cake/app/webroot/ 

be

RewriteBase /~username/dev/cake/

RewriteBasecares about mapping real urls to virtual ones, and I think that's what you want.

RewriteBase关心将真实网址映射到虚拟网址,我认为这就是您想要的。

回答by Simon

You're modifying the wrong file. The .htaccess file you need to modify is found in /dev, you shouldn't be modifying the one in /dev/cake/app/webroot/.htaccess

您正在修改错误的文件。您需要修改的 .htaccess 文件位于 /dev 中,您不应该修改 /dev/cake/app/webroot/.htaccess 中的文件

回答by Joe

I just want to point out that I THOUGHT the module was loaded because I saw the code but apparently it was not enabled!

我只想指出,我认为模块已加载,因为我看到了代码,但显然它没有启用!

To enable it in SSH (terminal) type the following command

要在 SSH(终端)中启用它,请键入以下命令

sudo a2enmod rewrite

The command below shows all modules that are active, if it appears in the list then you have it installed.

下面的命令显示所有处于活动状态的模块,如果它出现在列表中,那么您已经安装了它。

apache2ctl -M

Make sure to restart the server with...

确保重新启动服务器...

service apache2 restart

PhpCake should start working now.

PhpCake 现在应该开始工作了。

回答by David Z

Why not just extract the files to /usr/www/users/username/?

为什么不直接将文件解压到/usr/www/users/username/?

回答by David Z

i think the .htaccess is reading that '~' and thinking you are refering to your home folder (which is something of a shortcut in linux).

我认为 .htaccess 正在读取 '~' 并认为您指的是您的主文件夹(这是 linux 中的快捷方式)。

fastest way to get your app running is to go to cake's config/core.php and disable use of rewrite (it tells u to uncomment some stuff and delete some .htaccess file). the other way is to edit your .htaccess and somehow tell it '~' is not refering to home directory. maybe a simple \ will work.

让您的应用程序运行的最快方法是转到 cake 的 config/core.php 并禁用重写(它告诉您取消注释某些内容并删除某些 .htaccess 文件)。另一种方法是编辑您的 .htaccess 并以某种方式告诉它“~”不是指主目录。也许一个简单的 \ 会起作用。

回答by David Z

I am hosting on pair and I was able to get the rewrite rules to work but, I am having problems with the HTML->link getting the sort parameters for all links in the pages. Same code runs fine on my local machine running window but on pair sorting or too many query parameters (Edit or Update pages) causes problems.

我是成对托管的,并且能够使重写规则起作用,但是,我在 HTML-> 链接获取页面中所有链接的排序参数时遇到了问题。相同的代码在我的本地机器运行窗口上运行良好,但在对排序或太多查询参数(编辑或更新页面)上会导致问题。

回答by David Z

Yes, you have to give the DOCUMENT_ROOT path in the following files. Please refer to phpinfo() of your server to get correct DOCUMENT_ROOT path. It should be something like var/www/html/.

是的,您必须在以下文件中提供 DOCUMENT_ROOT 路径。请参考您服务器的 phpinfo() 以获取正确的 DOCUMENT_ROOT 路径。它应该是类似的东西var/www/html/

  1. .htaccess in the root /directory (Outside the app,cake,vendors directories)

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$ ) DOCUMENT_ROOT/app/webroot/    [L]
        RewriteRule    (.*) DOCUMENT_ROOT/app/webroot/ [L]
    </IfModule>
    
  2. .htaccess inside the /appdirectory

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$    DOCUMENT_ROOT/app/webroot/    [L]
        RewriteRule    (.*) DOCUMENT_ROOT/app/webroot/    [L]
    </IfModule>
    
  3. .htaccess inside the /app/webrootdirectory

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ DOCUMENT_ROOT/app/webroot/index.php?url= [QSA,L]
    </IfModule>
    
  1. .htaccess 在根/目录中(在 app、cake、vendors 目录之外)

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$ ) DOCUMENT_ROOT/app/webroot/    [L]
        RewriteRule    (.*) DOCUMENT_ROOT/app/webroot/ [L]
    </IfModule>
    
  2. /app目录中的.htaccess

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$    DOCUMENT_ROOT/app/webroot/    [L]
        RewriteRule    (.*) DOCUMENT_ROOT/app/webroot/    [L]
    </IfModule>
    
  3. /app/webroot目录中的.htaccess

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

It worked for me.

它对我有用。

回答by Copter

I had similar problem with 'advanced installation' from the official CakePHP Cockbook. After couple of hours I managed to get it working.

我在官方 CakePHP Cockbook 中遇到了类似的“高级安装”问题。几个小时后,我设法让它工作。

OS: Mac OS X

操作系统:Mac OS X

Cake core directory: /usr/lib/cake
My application app directory: /Users/myuser/dev/myapp
My application webroot directory: /Users/myuser/Sites/myapp

Cake核心目录:/usr/lib/cake
我的应用程序目录:/Users/myuser/dev/myapp
我的应用程序webroot目录:/Users/myuser/Sites/myapp

Modification of /Users/myuser/Sites/myapp/index.php

修改/Users/myuser/Sites/myapp/index.php

if (!defined('ROOT')) {
    define('ROOT', DS.'Users'.DS.'myuser'.DS.'dev');
}
if (!defined('APP_DIR')) {
    define('APP_DIR', 'myapp');
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
    define('CAKE_CORE_INCLUDE_PATH', DS.'usr'.DS.'lib');
}

Modification of /Users/myuser/Sites/myapp/.htaccess

修改/Users/myuser/Sites/myapp/.htaccess

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

As for /Users/myuser/dev/myapp/.htaccess- this file is not needed in this configuration so I deleted it. The most tricky part with this configuration is enabling mod_rewrite in Apache. On Mac editing /private/etc/apache2/httpd.confseems to be pointless (for this task). Additional Apache configuration file is created for each user.

至于/Users/myuser/dev/myapp/.htaccess- 此配置中不需要此文件,因此我将其删除。此配置最棘手的部分是在 Apache 中启用 mod_rewrite。在 Mac 上编辑/private/etc/apache2/httpd.conf似乎毫无意义(对于此任务)。为每个用户创建了额外的 Apache 配置文件。

Modification of /private/etc/apache2/users/myuser.conf

修改/private/etc/apache2/users/myuser.conf

<Directory "/Users/myuser/Sites/">
    Options FollowSymLinks
    AllowOverride All
</Directory>

By default AllowOverride is set to None, so mod_rewrite will not work in this configuration.

默认情况下 AllowOverride 设置为 None,因此 mod_rewrite 在此配置中不起作用。

After this changes http://localhost/~myuser/myappworks properly.

更改后http://localhost/~myuser/myapp正常工作。

PS. Quick permission fix for tmp directory.

附注。tmp 目录的快速权限修复。

sudo chgrp -R www /Users/myuser/dev/myapp/tmp
sudo chmod -R g+w /Users/myuser/dev/myapp/tmp