Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

iis

iis-7.5

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.

like image 762
Nathan Ridley Avatar asked Mar 21 '11 18:03

Nathan Ridley


3 Answers

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.

like image 159
Nathan Ridley Avatar answered Sep 29 '22 10:09

Nathan Ridley


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");
like image 29
voidstate Avatar answered Sep 29 '22 09:09

voidstate


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.

like image 36
user138720 Avatar answered Sep 29 '22 09:09

user138720