When PHP location redirect, doesn't.


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 inlcudes "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):


   $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);



The 310 is a reponse 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 my PHP file as a 2-bytes-per-character file. 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!

Top of page