Forum Moderators: coopster
if($doredirect){
mail("me@example.com","redir",$newurl);
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$newurl);
header("Connection: close");
}
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"
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?
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
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... ?
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.