When PHP location redirect, doesn’t.
If your header location redirect isn’t working, check your PHP filetype. Actually, many weird stubbornesses of PHP might be prompted by this gotcha.
Are you having trouble with a PHP Header Location? Does your browser stubbornly say “Warning: Cannot modify header information – headers already sent by …”. If so, first ensure that your PHP hasn’t issued any output, which includes “empty” lines. Then if that doesn’t explain it, my bet is that you have got a Unicode or UTF-8 filetype.
I had some files which were receiving a nice lot of hits from Google, but were placed inconveniently. I wanted to move them, so adopted the regular technology which is (as you may know):
<?php $host = $_SERVER['HTTP_HOST']; $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); $newpage = 'index.php?tab_id=4&target=20'; header("Location: http://$host$uri/$newpage", true, 301); exit; ?>
The 310 is a response code saying that the page has moved permanently. If you are lucky, Google will take note and eventually update it’s index. (In any case you should go visit Google Webmasters pages and tell them about the page moving.)
However in this case I had an annoying snag. I kept getting the browser message: “Warning: Cannot modify header information – headers already sent by..”. After a while I figured the answer was that I had inadvertently got my PHP file as a 2-bytes-per-character file. To the PHP parser, the file would “look” something like this:
< ? p h p $ h o s t = $ _ S E R V E R [ ' H T T P _ H O S T ' ] ;
Unusually, I had downloaded my own file back from the webserver by FTP, instead of fetching my local copy. I use the fantastic editorВ UltraEditВ and it showed ‘U8-DOS’ in the status bar. So when the file was being delivered to the browser, a couple ‘magic bytes’ were being delivered first: invisible to me but they tell the server that this is a UTF8 file; these bytes were enough to count as ‘output’ and derail the process.
Change your file back to single-byte type (ASCII) and you’ll be fine. How to do that? I dunno apart from use UltraEdit! (menu > File > Conversions and then Unicode to ASCII or UTF-8 to ASCII).