0

After moving my website to a webhost which uses LiteSpeed it seems PHP header is ignored.

I have tried these variations:

  • $s = "HTTP/1.1 404 Not Found";
  • $s = "HTTP/1.0 404 Not Found";
  • $s = "Status: 404 Not Found";
  • $s = $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found';

followed by

header($s)

But on LiteSpeed these all seem to be ignored.

  1. Is there any option in LiteSpeed I can turn off to get Apache compatible behavior? I have access to cPanel and .htaccess
  2. Any other possible explanation and/or solution?

NOTES:

  • Problem happens on both https and http variations
  • The page returning 404 is a page used for handling mod_rewrites (so for URL queries where no valid content is found in database, 404 http response code should be returned)
  • Problem also happens on another domain of mine placed the server (using a popular PHP forum solution with a mod_rewrite based plugin for nice looking URLs)
  • The mod_rewrites themselves work fine.
  • The header() call is also ignored on non-mod_rewrite URLs such as test?id=2
  • Both sites work fine on those Apache servers I have tried through time (at least two different Apache webhosts + my localhost)

Also, on both websites I have disable LiteSpeed caching in my .htaccss file

<IfModule LiteSpeed>
RewriteRule .* - [E=Cache-Control:no-cache]
</IfModule>

The problem can be duplicated by creating a simple

test.php file like this

<?php
  $s = $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found';
  header($s);
  echo 'test';
?>  

And then try request test?id=2

Tom
  • 95
  • 6

2 Answers2

0

Most of the time, LSWS will behave the same as apache.

If you have root ssh access to the server or WHM control panel, you can try to switch between apache and LSWS to see if they behave the same.

switch to apache: /usr/local/lsws/admin/misc/cp_switch_ws.sh apache switch to LSWS /usr/local/lsws/admin/misc/cp_switch_ws.sh lsws

If so, you may need to check the your PHP code or the rewrite rules to see where is wrong.

If Apache and LSWS behave differently, you can log a ticket.

  • For reference, the mod_rewrites work fine. It is only the header() call in PHP which is behaves differently - i.e. gets ignored. Not sure if related at all, but I am trying to troubleshoot any way possible. The header() in my own code and the PHP forum code works well on Apache/localhost and Apache/earlier-webhost. That said, I will see if I can try some of the things you suggest :) – Tom Sep 21 '18 at 16:44
0

NotePad++ was configured to use "Encode with UTF8" and not "Encode with UTF8 without BOM"

This meant some files included an "invisible" leading BOM.

I had been inspecting HTTP request and response in browser-developer-mode, but there the BOM was invisible.

...

header() in PHP will (of course) not work if you already output anything before headers

...

I suspect the reason I never had this issue on Apache is that autofixes it

Tom
  • 95
  • 6