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

发布于 2024-12-14 09:07:21 字数 1925 浏览 1 评论 0原文

http://www.hesa.ac.uk 上,我们使用自定义 404 处理脚本来重定向例如,http://www.hesa.ac.uk/press 的用户实际的 URL,这是一个丑陋的 CMS:http://www .hesa.ac.uk/index.php?option=com_content&task=category&sectionid=1&id=1&Itemid=161

我们正在运行快速cgi。

发送 301 标头,然后发送位置标头。

它对于 99% 的用户来说工作正常,但其中一些用户报告加载时间为 5 到 6 秒。我们认为,这是由于重定向中出现了一些杂散内容:

<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>

这不会在我们可以看到的代码中的任何位置输出。这是实际执行重定向的方法:

    /**
 * 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();



}

我似乎找不到任何指示如何停止输出这些额外内容的资源。我尝试了上述方法的各种变体来进行重定向。

有人遇到过类似的问题吗?有人有什么想法吗?

干杯,

G

编辑:我们已经意识到这是 IIs7 的预期行为,但 IIS6 从未使用相同的代码执行此操作,无论是否预期,我们的用户都在抱怨,这似乎是问题所在。

编辑2:似乎唯一可行的解​​决方案是放弃这种方法,转而使用 IIS7 的 url 重写功能,这需要编写一个克隆 PHP 类功能的 C# 类,然后将其插入 IIS。

编辑3:但是,设置 content-length: 0 标头可能会有所帮助。不过还没有测试过。

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§ionid=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&task=category&sectionid=1&id=1&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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2024-12-21 09:07:21

事实证明,问题实际上是在重定向代码中使用了内置 PHP 函数 getHostByAddr()。没有正确设置 DNS 记录的客户端主机会在 getHostByAddr() 中遇到 5 到 6 秒的延迟(在 IIS7 下,我们在 IIS6 中从未遇到过此问题)。如果您找到替代功能并将其替换,问题就会消失。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文