This project is read-only.

PHP header function - redirection does not work using header("Location: url")

Topics: faq
Apr 5, 2013 at 8:50 AM
Edited Apr 5, 2013 at 8:51 AM
Good day everyone.

We are using this project to implement a local testing environment for websites and notably PHP pages, and we got it working rather well. Thank you for the powerful solution. But we've stumbled on the fact that the function
does not result in redirecting. The function works it seems, browser receives new headers, including Location, but no redirection happens. I simplified the code to the following (to avoid any possible output etc., and there are no error reports whatsoever)

// header("Content-type: image/png;"); // this works, for example
header('Location:'); // this doesn't, though it works with easyPHP etc.

I didn't find any posts here related to this. The help would be very appreciated.

Apr 5, 2013 at 9:04 AM
Which HTTP status code is returned?

Apr 5, 2013 at 9:06 AM
Edited Apr 5, 2013 at 9:07 AM
If we are talking about the same thing, it's
"200 Made by Jonas Gauffin"
it's what I see in FireBug" in status column. It is the only visible difference I see compared to other servers executing the same page..

EDIT easyPHP, for example, returns "302 Found"
Apr 5, 2013 at 9:09 AM
Ok. It needs to be 302 to tell that it's a redirect. I guess that the PHP module needs to check if the location header has been set and if so change the status code to 302.

Apr 5, 2013 at 9:10 AM
You can also try to change it in php (just to validate that it works then):

Apr 5, 2013 at 9:20 AM
I've already tied to set the Status header, and I've tried the code from StackOverflow, as follows:
header("Status: 302"); // for fast cgi
header($_SERVER['SERVER_PROTOCOL'] . " 302");
header("Status: 302 Found"); // for fast cgi
header($_SERVER['SERVER_PROTOCOL'] . " 302 Found");
and in FireBug I see that Status header is sent, but "Status of received responces" (Status column in Network output) is still "200 Made by Jonas Gauffin".

Could you please hint me where to make the changes in the source code?

Thank you for your support
Apr 5, 2013 at 9:26 AM
P.S. I think I found it, in Response.cs Response() method. I will modify and post here the results..
Apr 5, 2013 at 9:58 AM
Edited Apr 5, 2013 at 10:03 AM
If you are using the code from :

Open CgiHandler.cs and add the 302 to the GetStatusCode method:
private static HttpStatusCode GetStatusCode(string value)
    if (value.StartsWith("404"))
        return HttpStatusCode.NotFound;
    else if (value.StartsWith("401"))
        return HttpStatusCode.Unauthorized;
    else if (value.StartsWith("302"))
        return HttpStatusCode.Redirect;

    return HttpStatusCode.OK;
This should fix the problem.

EDIT: just saw that the online version is not up to date, so the above won't work. Will be updated soon...
Apr 5, 2013 at 10:08 AM
I think we use another version; there's no such function in CgiHeaders.cs, but there's ParseCgiHeaders() (with several ToDo comments), I will try to look into it..
Apr 5, 2013 at 10:31 AM
Edited Apr 5, 2013 at 10:32 AM
Ok I think I made it work.

We use the version from here:
downloaded somewhere in March 2013.

I added the code to CgiHeaders.cs ParseCgiHeaders() function (see code between // DD START and // DD END comments):
        public static void ParseCgiHeaders(ref string cgiOutput, IResponse response)
            // TODO: Make this more robust (are we really stripping headers???)
            int index = cgiOutput.IndexOf("\r\n\r\n");

            if (index != -1)
                string header = cgiOutput.Substring(0, index + 2);
                cgiOutput = cgiOutput.Substring(index + 2);

                int end = header.IndexOf("\r\n");

                while (end != -1)
                    string line = header.Substring(0, end);
                    header = header.Substring(end + 2);

                    int colonIndex = line.IndexOf(":");
                    if (colonIndex <= 1)

                    string val = line.Substring(colonIndex + 1).Trim();
                    string name = line.Substring(0, colonIndex).Trim();

                    // TODO !!!
                    if (name.ToLowerInvariant() == "content-type")
                        response.ContentType.Value = val;
                    // DD START fix redirecion by Location header:
                    else if (name.ToLowerInvariant() == "location")
                    // DD END fix redirecion by Location header
                        response.Add(new StringHeader(name, val));

                    end = header.IndexOf("\r\n");

            cgiOutput = cgiOutput.Trim();
Thank you everyone, I would have had heavy difficulties finding it on my own..

Apr 5, 2013 at 11:58 AM
I updated the code. There are some breaking changes, so also have a look at the example on the webpage.

Regarding your problem: the code for setting the CGI headers is now in CgiModule.cs in the HttpServer.Addons.Modules namespace.
Apr 5, 2013 at 12:31 PM
Edited Apr 5, 2013 at 1:02 PM
Thank you. Have you also fixed an issue with missing POST data? I had to fix it locally, it's more or less along the lines of this thread:
(EDIT: our version may be obsolete compared to latest releases)
Apr 5, 2013 at 12:46 PM
Edited Apr 5, 2013 at 12:46 PM
P.S. Sorry, POST data fixes were done in HttpServer itself, not in Addons, so it's not related to your last post.
Apr 5, 2013 at 12:54 PM
Edited Apr 5, 2013 at 12:55 PM