【问题标题】:PHP and mod_rewrite problems on PleskPlesk 上的 PHP 和 mod_rewrite 问题
【发布时间】:2012-08-26 12:40:23
【问题描述】:

我束手无策...我正在尝试做一些简单的事情,但我不明白出了什么问题...

我只是试图将所有请求带到域,并将它们路由到 index.php

为清楚起见进行编辑:index.php 作为请求的调度程序,作为框架的一部分。

我在本地机器上可以正常工作,但在服务器上(带有 Plesk 的 VPS Linux)我遇到了各种各样的问题...

为清楚起见进行编辑:这些规则在虚拟主机的 vhost.conf 配置文件中定义。

这是 mod_rewrite:

<IfModule rewrite_module>
    RewriteEngine On
    RewriteRule ^/.* /index.php
</IfModule>

这是我尝试访问“www.mydomain.com/home/index”时的 apache 错误日志(例如):

[debug] core.c(3072): [client xxx.xxx.xxx.xxx] r->uri = /phppath/cgi_wrapper/phppath/cgi_wrapper/phppath/cgi_wrapper/home/index
[debug] core.c(3078): [client xxx.xxx.xxx.xxx] redirected from r->uri = /phppath/cgi_wrapper/phppath/cgi_wrapper/home/index
[debug] core.c(3078): [client xxx.xxx.xxx.xxx] redirected from r->uri = /phppath/cgi_wrapper/home/index
[debug] core.c(3078): [client xxx.xxx.xxx.xxx] redirected from r->uri = /home/index

从跟踪中可以看出,它似乎将 /home/index 重定向到 /phppath/cgi_wrapper/,然后似乎又被传递到 /phppath/cgi_wrapper/phppath/cgi_wrapper/home/index,等等直到达到最大内部重定向。

一个 HTTP 500 被发送到浏览器。

进一步编辑 - 额外信息。从我从 Plesk 分散文件的方式中可以看出,这是对虚拟主机有影响的仅有的两行。我认为这可能是那个 Action 声明......我可以在 vhost.conf 中以某种方式覆盖它吗?

in php-cgi.conf:
ScriptAlias /phppath/ "/var/www/cgi-bin/cgi_wrapper/"
Action php-script /phppath/cgi_wrapper

in php.conf:
AddHandler php5-script .php

进一步编辑:在动态生成的 conf 文件中找到了这个(在虚拟主机级别)。

<Files ~ (\.php)>
    SetHandler None
    AddHandler php-script .php
    Options +ExecCGI
    allow from all
</Files>

有人对正在发生的事情有任何想法吗?我已经拉头发两天了...提前谢谢

【问题讨论】:

  • 您为什么要将所有请求重定向到一个页面?如果您描述了您想要实现的目标,它将帮助人们提供建议。
  • 另外,你能告诉我们这些规则是在站点公共根目录的 .htaccess 文件中,还是在适用于整个服务器的 httpd.conf 文件中?
  • 嗨 - 我正在尝试将请求路由到单个页面,因为该页面充当调度程序。它是框架的一部分。规则直接在 vhost.conf 文件中定义(其中为每个虚拟主机定义了文档根目录、服务器名称等)
  • 嗨,我已经尝试过了,但我仍然在 apache 日志中得到相同的结果。我不认为 mod_rewrite 语法有问题 - 正如我所说,它在本地运行良好。我想了解为什么我在 apache 日志中看到那些奇怪的消息,以及重定向的“堆积”。值得注意的是,如果我将 RewriteRule 中的“index.php”替换为“index.html”(其中 index.html 是一个静态 html 文件),则重定向工作正常。

标签: php apache mod-rewrite plesk


【解决方案1】:

检查您的服务器是否支持.htaccess 中的mod_rewrite,或者您可能必须使用以下代码编写web.config 文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="false"/>
        <rewrite>
            <rules>
            <rule name="APPLICATION: https://<your-host-url>/" patternSyntax="Wildcard">
                <match url="*"/>
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                    </conditions>
                <action type="Rewrite" url="index.php"/>
            </rule></rules>
        </rewrite>
    </system.webServer>
</configuration>

【讨论】:

    【解决方案2】:

    经过多次撕扯头发,咬牙切齿,又敲了三下键盘……我终于找到了可行的方法。它不漂亮,但它确实有效......如果有人可以对此提供任何改进,请做 - 我全神贯注。或者眼睛。原来如此。

    所以,我首先注意到 Apache 日志中反复出现的 /phppath/cgi_wrapper/home/index 问题,这让我想到:“也许我不应该将 /home/index 传递给 PHP CGI,但我应该传递 index.php”。因此,我将 Rewrite 修改如下:

    <IfModule rewrite_module>
        RewriteEngine On
        RewriteLog /blah/blah/blah/rewrite_log
        RewriteLogLevel 5
    
        RewriteRule ^.*$ /index.php [PT]
    </IfModule>
    

    我只是添加了 PassThrough (PT) 标志,根据文档,它将重写结果传递给处理程序。

    这让我得到了以下 apache error_log

    [debug] core.c(3078): [client xxx.xxx.xxx.xxx] redirected from r->uri = /index.php
    [debug] core.c(3078): [client xxx.xxx.xxx.xxx] redirected from r->uri = /index.php
    [debug] core.c(3078): [client xxx.xxx.xxx.xxx] redirected from r->uri = /index.php
    

    我认为这是一种改进,但它仍然不起作用。

    然后我去我闪闪发光的新 rewrite_log 看看这是否能照亮我。我看到了:

    xxx.xxx.xxx.xxx - - [28/Aug/2012:15:38:05 +0400] [www.mydomain.com/sid#7f3c39001360][rid#7f3c390bbc88/initial] (2) rewrite '/' -> '/index.php'
    xxx.xxx.xxx.xxx - - [28/Aug/2012:15:38:05 +0400] [www.mydomain.com/sid#7f3c39001360][rid#7f3c390bbc88/initial] (2) forcing '/index.php' to get passed through to next API URI-to-filename handler
    xxx.xxx.xxx.xxx - - [28/Aug/2012:15:38:05 +0400] [www.mydomain.com/sid#7f3c39001360][rid#7f3c390c2710/initial/redir#1] (2) init rewrite engine with requested uri /phppath/cgi_wrapper/index.php
    xxx.xxx.xxx.xxx - - [28/Aug/2012:15:38:05 +0400] [www.mydomain.com/sid#7f3c39001360][rid#7f3c390c2710/initial/redir#1] (3) applying pattern '^/(.*)$' to uri '/phppath/cgi_wrapper/index.php'
    

    因此,由此我推断出我遇到了同样的问题:/phppath/cgi_wrapper/index.php 现在再次被传递到重写引擎以进行重写,而这又与/index.php 一起出现,它正在被传回,等等等等。

    所以我添加了一个条件。 (这对我来说似乎很难看;我相信有更好的方法):

    <IfModule rewrite_module>
        RewriteEngine On
        RewriteLog /blah/blah/blah/rewrite_log
        RewriteLogLevel 5
    
        RewriteCond ${REQUEST_URI} !^/phppath
        RewriteRule ^.*$ /index.php [PT]
    </IfModule>   
    

    RewriteCond 基本上是在说:如果请求不以 /phppath 开头,则应用以下规则。

    就是这样。现在它正在工作;根据需要,所有请求都由 index.php 处理。

    【讨论】:

      猜你喜欢
      • 2013-04-05
      • 1970-01-01
      • 2013-05-31
      • 2011-12-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多