locked
PHP headers and redirect RRS feed

  • Question

  • header code on the Expression web server will not function correctly. Works fine in apache.
    header('Location: final.php');
    

    Will not redirect in any browser if I use F12 to preview. No problem with Apache.

    I examined headers and it gives a 302 status to the browser and the correct website. However it will not redirect. Maybe this is something that can be corrected or I'm doing something wrong.

    In the worst case I can just refresh the browser after I save, I think it is a bit faster anyway.
    Thursday, October 1, 2009 11:22 PM

Answers

  • 200 OK is not a redirection code, and Status: 302 Moved Temporarily is not a status code, but an invalid header. While the PHP documentation states that header('Location: ...') is a "special case" as it automatically sets a 302 status code, you should try to set the status code yourself:

    <?php
    header('HTTP/1.0 302 Found');
    header('Location: ... ');
    exit;
    ?>

    Note that the first statement does not read header('Status: HTTP/1.0 302 Found');.

    To set the preview URL, go to Site > Site Settings...> Preview > Preview using custom URL for this website.
    • Marked as answer by windowzdude Friday, October 2, 2009 3:54 PM
    Friday, October 2, 2009 1:30 PM

All replies

  • The Location: header requires an absolute URL, not a relative one to begin with.

    How did you check the HTTP headers? Post the complete headers here (and maybe the response body, too). If the server response says 302, a browser should follow the redirection, and there's nothing more the server can do.

    Besides that, I would not use xWeb's development server to test PHP code. Install Apache on your local machine if you haven't already done so and configure the preview URL in xWeb accordingly.

    Friday, October 2, 2009 12:06 PM
  • Server: Microsoft Expression Development Server/3.0.0.0
    Date: Fri, 02 Oct 2009 13:13:48 GMT
    Status: 302 Moved Temporarily
    X-Powered-By: PHP/5.3.0
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Location: final.php
    Content-Type: text/html
    Connection: Close
    
    200 OK
    The headers come from FireFox web developer tool bar. I tried it with absolute URLS too, it wont work either. I see its better to put the project in the htdocs directory and configure there. Where do you set the preview URL in EW?

    I guess the server is geared toward ASP development, or maybe just static web pages?
    Friday, October 2, 2009 1:18 PM
  • 200 OK is not a redirection code, and Status: 302 Moved Temporarily is not a status code, but an invalid header. While the PHP documentation states that header('Location: ...') is a "special case" as it automatically sets a 302 status code, you should try to set the status code yourself:

    <?php
    header('HTTP/1.0 302 Found');
    header('Location: ... ');
    exit;
    ?>

    Note that the first statement does not read header('Status: HTTP/1.0 302 Found');.

    To set the preview URL, go to Site > Site Settings...> Preview > Preview using custom URL for this website.
    • Marked as answer by windowzdude Friday, October 2, 2009 3:54 PM
    Friday, October 2, 2009 1:30 PM
  • Not true.  You can use a relative URL the way the OP had done.  Will work without a problem.  The problem is most likely with the EW development server.  If the OP had used IIS or a local version of Apache (using WAMP) it will work.

    To the OP: The EW development server is not a production server so don't expect it to act like one.  Download WAMP and test your websites in a production like environment.
    --
    Chris Hanscom - Microsoft MVP
    On Facebook | On Twitter | Resource Center | Veign's Blog | Web Development Help

    Get a Complete Website Analysis by Veign
    Friday, October 2, 2009 2:11 PM
  • You are wrong, the HTTP specification is absolutely clear in this aspect. It doesn't matter that some browsers "support" relative URLs in this header.
    Friday, October 2, 2009 2:22 PM
  • I guess my statement was about it working even though it should not be used.  You are correct Chris and I will fix my login script.  Since its always worked without a problem I thought it was allowed.

    Thanx for bringing this mistake to my attention.
    --
    Chris Hanscom - Microsoft MVP
    On Facebook | On Twitter | Resource Center | Veign's Blog | Web Development Help

    Get a Complete Website Analysis by Veign
    Friday, October 2, 2009 2:34 PM
  • Lots of books use forms of relative url and I'm glad I see the correct way now. I'm going to use Apache as I was doing before. Thanks!
    Friday, October 2, 2009 3:56 PM
  • I have read the responses but I still cannot get header() to work in EW 3 Preview:

    using http://localhost:21192/@redirect_test_target.php/ works fine in IE 8 browser

    but using the EW 3 Preview with IE 8 and using XAMPP apache

    header('Location: http://localhost:21192/@redirect_test_target.php/'); does not work.

    What do I have to do to make the redirect to work?


    Dennis M Reed (http://dmreed.com)
    Sunday, December 26, 2010 11:54 AM
  • make sure there is no space before the opening <?php tag in the file.
    the <?php tag has to be the very first character in the file.
     
    after you do a header, you should do a 'exit;' so that PHP does not
    continue executing
     

    Best Wishes, Singapore Web Hosting http://sgwebsitehosting.com/
    • Proposed as answer by wild-webs.com Friday, December 30, 2011 4:01 AM
    • Unproposed as answer by wild-webs.com Friday, December 30, 2011 4:02 AM
    Sunday, December 26, 2010 1:03 PM
  • I had a similar problem creating a PHP redirect in MS Expression Web. It worked on 2 test servers (Apache and Win 7) but would not work on the production server. The redirect was skipped over. This same behavior existed in a basic php file which only contained:

    <?php header('Location: http://www.yahoo.com/'); ?>

    The only solution I could find was to copy the entire contents of the php file created in MS Expression Web, paste it into an ASCII editor, and re-save it as the original file. Once this was done and the file uploaded to the production server the redirect worked.

    Hope this works for you...but not an ideal solution by any stretch.

    Friday, December 30, 2011 4:12 AM
  • This post has been dead for over a year. Who do you think you're talking to???

    Never resurrect a dead thread, in any forum--that's general knowledge and a universal truth. It's been the rule on the Internet since Teddy Roosevelt's administration.

    Sheesh.


    Comic Sans walks into a bar. The bartender says, "Get out! We don't serve your type."
    Friday, December 30, 2011 4:28 AM