douxingti9307 2011-11-08 09:46
浏览 50
已采纳

使用PHP 5.3.5 / IIS7将默认内容添加到301标头中

On http://www.hesa.ac.uk, we are using a custom 404 handling script to redirect users from, for exmaple, http://www.hesa.ac.uk/press to the actual URL, which is an ugly CMS one: http://www.hesa.ac.uk/index.php?option=com_content&task=category&sectionid=1&id=1&Itemid=161

We're running fast-cgi.

A 301 header is sent, then a location header.

It works fine for 99% of our users but some of them are reporting 5 to 6 second loading times. This is, we think, due to a bit of stray content which is turning up in the redirection:

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://www.hesa.ac.uk/index.php?option=com_content&amp;task=category&amp;sectionid=1&amp;id=1&amp;Itemid=161">here</a></body>

This isn't output anywhere in the code that we can see. Here's the method which actually does the redirection:

    /**
 * Do the actual redirection to the win URL
 *
 */
function doRedirection() {


        //start output buffering to trap any content that is output by default by the headers
        @ob_start();

        //permanently moved header
        //header( 'HTTP/1.1 301 Moved Permanently' );

        //fast-cgi, so use this:        
        header('Status: 301 Moved Permanently', true);              

        @ob_end_clean(); // clear output buffer

        //location header
        header( "Location: ". $this->winUrl );  

        @ob_end_clean(); // clear output buffer

        die();



}

I cannot seem to find any resource which indicates how to stop this extra bit of content being output. I have tried various variations on the method above to do the redirection.

Has anyone had a similar problem? Does anyone have any ideas?

Cheers,

G

EDIT: we've become aware that this is expected behaviour for IIs7, but IIS6 never used to do it with the same code, and whether it's expected or not, our users are complaining and this seems to be the issue.

EDIT 2: it seems that the only workable solution is to abandon this approach and instead move to IIS7's url rewriting functionality, which entails writing a C# class which clones the functionality of the PHP class then plugging that into IIS.

EDIT 3: HOWEVER, setting a content-length: 0 header might possibly help. Not tested yet though.

  • 写回答

1条回答 默认 最新

  • dongyan0629 2011-11-16 15:58
    关注

    Turns out the problem was actually the use of the built-in PHP function getHostByAddr() within the redirection code. Client hosts which do not have their DNS records set up properly experience a 5 to 6 second delay from getHostByAddr() (under IIS7, we never had this issues with IIS6). If you find an alternative function and swap it out, the problem disppears.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog