Forum Moderators: coopster

Message Too Old, No Replies

header() redirection not working. in wordpress

i hope this is a simple problem

         

httpwebwitch

4:14 am on Jun 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



my php code:

if($doredirect){
mail("me@example.com","redir",$newurl);
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$newurl);
header("Connection: close");
}

where $newurl is ... the new url. it is correct - and the email does arrive!

this code is being prepended to the page using this setting in the .htaccess of the parent directory:

php_value auto_prepend_file "/home/myserver/public_html/auto_prepend.inc"

I suspected something might be amiss in the .htaccess, so, here it is in its entirety:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^scripts/(.*)$ /scripts.php?path=$1

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

the redirection isn't happening. is there anything incorrect here that would stymie a header() redirection? or elsewhere in wordpress?

eelixduppy

3:15 pm on Jun 12, 2008 (gmt 0)



Are you getting any errors from this script? What would prevent a redirect is if the headers were already sent to the browser before the header() call, in which case an error would result.

httpwebwitch

5:29 pm on Jun 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



no errors - nothing! if it was characters sent before header(), I'd see exceptional output.

The redirection there is a kind of URL cleanser, for redirecting to a canonicalized URL. It just does one thing: check if the url needs to be canonicalized, and if it does, it redirects w/301 to the new URL.

Since this PHP is prepended with the "auto_prepend_file" thing, I *believe* it's the first thing that happens on every page, so I can't imagine any other headers would preexist.

I'm suspicious of WP. This is happening in at least a couple of my Wordpress blogs, all on the same server, with the same prepended script. It's got to be something nestled in the warm dark crannies of Wordpress configuration.

thanks for the quick reply eelixduppy

httpwebwitch

4:03 pm on Jun 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What settings in PHP and/or Apache could possibly clobber a header() redirection, without killing the page with an error?

I have looked through the WP code for header-related stuff, but I don't know what else I should be searching for. Oh! Maybe there's something in Wordpress that overrides error messages! could that be it... ?

httpwebwitch

4:32 pm on Jun 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



just before the header() commands, I added this line:
echo "ok";

and suddenly I see the "Warning: Cannot modify header information - headers already sent" error.

so, at least I know that's working.

httpwebwitch

5:21 pm on Jun 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You know, the reason I post debugging narratives here is to help the entire world. There's a benevolence involved; not only do I believe that someone in the community will have the answer and divulge it, but I know that future developers with the same problem will be inspired and ultimately epiphanized by my struggle and its very public conclusion, whether culminates in success or failure.

Sometimes my "help me" questions are truly confounding. Experts scratch their heads. PHP savants commiserate and rack their brains to find the solution, because they consider it an intellectual challenge, like a tantalizing sudoku.

Other times, its just that I forgot a $@#! semicolon.

So it does not embarass me at all when the answer to "why is my TV screen black" is "plug it in". It's for the greater good.

Here's what I had before:

header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$newurl);
header("Connection: close");

this is what I changed it to:

header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$newurl);
header("Connection: close");
[red]exit;[/red]

it works now.

The Lesson I Learned From This
when you use header(), it doesn't just add the header and flush the output. Of course not! There's still content to be rendered to go along with those headers. It's not like using return in a function. It adds the header, then it may go on and do a bunch of other things, like adding other headers. Or stomping on the ones you created. If you want to redirect using a header, use exit right after making the headers.

die() would have worked too

naturally, Wordpress does things to serve up the right status for its pages. That's why the header() wasn't prompting a redirect, but it wasn't causing an error either.

I feel smarter now.

cam8001

1:26 am on Jun 18, 2008 (gmt 0)

10+ Year Member



You are a scholar and a gentleman. I had been getting very perplexed and was beginning to doubt my ability until I read your solution.

Thank you again kind sir.

httpwebwitch

2:12 am on Jun 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



why thank you, and welcome to WebmasterWorld!