简体   繁体   中英

PHP - header() redirect: Firebug logs 404 - why?

I am using a PHP redirect in one of my projects. I do it like this:

header( 'Location: http://domain.net/?cig=warn&onum=' . $onum . '&cnum=' . $cnum . '&c=' . $c . '#Form' );

So you see, it's a normal use of PHP's header() function where some variables are included in the URL.
It's working, but testing the page with Firebug, I get this error in the console:

"NetworkError: 404 Not Found - http://domain.net/?cig=warn&onum=12&cnum=73&c=ui#Form"

Is there actually something wrong with the way I do it or is this just Firebug being picky because of all the parameters and the anchor?

Since this page lives within a WordPress environment, you must force a 200 status code by manually setting it before your headers get sent (using the WordPress status_header() function):

<?php
// This must appear before any output is sent!
status_header(200);
?>

You'll have to pull in the WordPress functions to get at this, obviously. The problem is that WordPress is intercepting the request before your page has a chance to process it. It cannot locate the "page" and sets a 404 response code, which is what eventually gets sent back out.

When I've had to pull in the WordPress functions in the past, I've done so by including these two lines on the page:

define('WP_USE_THEMES', false);
require_once("/path/to/my/site/wp-blog-header.php");

Now, that said, the pages I've done this with in the past have had the same look and feel as the ones in my WordPress installation. There may be a better way to pull in the functions alone; you'd have to consult the WordPress documentation to find out (I don't know if there is). How to do it properly might be a good question for the WordPress StackExchange site .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM