PHP on IIS7 - Receiving “Object Moved” html page instead of actually redirecting

若如初见. 提交于 2019-12-07 02:56:49

问题


I'm learning PHP on my computer with IIS7.5 as the web server and am having a problem completing a 301 redirect correctly.

The tutorials and forums all say to use the following:

Header('Location: ' . $url, true, 301);

OR

Header('Location: ' . $url);

In both cases, instead of actually redirecting, the browser (Chrome and Firefox) display this:

Object Moved

This document may be found here

Using the FireFox web developer toolbar, I retrieved the page headers, which were:

Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.5, ASP.NET
Date: Mon, 21 Mar 2011 18:47:35 GMT
Content-Length: 123

301 Moved Permanently

Why is the page not redirecting? Displaying that page is kind of redundant and annoying for users.


回答1:


I figured it out. The Location header must be an absolute path to auto-redirect. If it's a relative path it doesn't redirect.




回答2:


I had a similar issue but the path was already absolute. I solved it by specifically sending a 301 header before the location. PHP is supposed to detect redirects and do this automatically but wasn't doing.

header("HTTP/1.1 301 Moved Permanently");
header("location:http://www.mysite.com/mypage.php");



回答3:


I had this problem using PHP on IIS7 using absolute URL. Bugged me for a little while. Ensure that you put exit(); after your header('Location: https://domain.tld/resource'); the header doesn't stop execution, and the function will otherwise return somewhere giving maybe unexpected results.




回答4:


I figured out a easy hack to fix this. Use an Outbound Redirect rule to set the Content Length header in redirect responses to 0. Browsers will truncate the sent response body automatically based on the Content Length header.

Here is my redirect rule:

<rewrite>
    <outboundRules>
        <rule name="Remove Content from Redirect Requests" preCondition="Is30xStatus" enabled="true" patternSyntax="Wildcard">
            <match serverVariable="RESPONSE_CONTENT_LENGTH" pattern="*" />
            <action type="Rewrite" value="0" />
        </rule>
        <preConditions>
            <preCondition name="Is30xStatus">
                <add input="{RESPONSE_STATUS}" pattern="^30[1-8]$" />
            </preCondition>
        </preConditions>
    </outboundRules>
</rewrite>

I would recommend using that as a reference to create a new outbound rewrite rule using the IIS Manager as editing web.config files directly can have unexpected results.

Update: Apparently there is a PHP directive that changes the format of the headers to help with this as well:

cgi.rfc2616_headers = 1

I have tested this on my server, and this seems to help the issue as well.



来源:https://stackoverflow.com/questions/5382219/php-on-iis7-receiving-object-moved-html-page-instead-of-actually-redirecting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!