PHP header() being ignored after switching to LiteSpeed webhost

#1
After moving my websites to a webhost (shared) 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 while it works on my localhost/Apache and worked on my earlier webhost/Apache. I use the above on a page which handles URL requests that have gone through a mod_rewrite... which is why I need to use header() for reporting when the requested resource is not found. (If it exists appropriate content is instead loaded.)

...

Also worth noting, on both websites I have disabled caching in my .htaccss file
Code:
    <IfModule LiteSpeed>
    RewriteRule .* - [E=Cache-Control:no-cache]
    </IfModule>
...

also have another website/domain running SMF small machines forum with a "clean urls" module installed (which uses mod_rewrite to support nice looking URLs for forum posts) ... That now also returns "200 - found" for *any* URL - e.g. forum.example.com/flsflisdjlgijsd - while in the past it worked.

...
So I have two very different sites which both suffer from he same problem after switching to a LiteSpeed webhost., and I am getting "soft 404 errors" in Google Webmaster Tools for those two domains now :(
  1. Is there any option in LiteSpeed I can turn off to get Apache compatible behavior? I have access to cPanel and .htaccess (shared webhost though so there are limits)
  2. Any other possible explanation and/or solution? Maybe something LiteSpeed is more "harsh" about than Apache or similar?
 
Last edited by a moderator:
#2
Can anyone here duplicate my test case:
1) Using mod_rewrite: Can you successfully output a HTTP header response using PHP header() function?
2) Using mod_rewrite + disable all caching in .htacces: Can you successfully output a HTTP header response using PHP header() function?

If yes =
My server setup is somehow at fault (open to ideas what to write them) or some weird code problem which happens both in my code and external forum code.

if no =
problem (supposedly) identified with LiteSpeed or something closely related to it

...

Note: Let me know if you need more information
 
#3
For what it is worth,

the problem also manifests itself without mod_rewrite with a simple page like this test.php file
PHP:
<?php
  $s = $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found';
  header($s);
  echo 'test';
?>
And then simply try request test?id=2
 
Last edited by a moderator:

Pong

Well-Known Member
Staff member
#4
I tested your code:
PHP:
<?php
$s = $_SERVER["SERVER_PROTOCOL"].'404 Not Found';
header($s);
echo 'test';
?>
on Apache, your code generated 500 error:

I run http://example.com/test3.php

Code:
Request URL: http://example.com/test3.php
Request Method: GET
Status Code: 500 Internal Server Error
Remote Address: 13.2.17.10:80
Referrer Policy: no-referrer-when-downgrade
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
Date: Mon, 24 Sep 2018 20:41:15 GMT
Keep-Alive: timeout=5, max=100
Server: Apache
Transfer-Encoding: chunked
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Host: example.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
[/QUOTE]

On LiteSpeed:
[QUOTE]
Request URL: http://example.com/test3.php
Request Method: GET
Status Code: 200 OK
Remote Address: 13.2.17.10:80
Referrer Policy: no-referrer-when-downgrade
Accept-Ranges: bytes
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 24
Content-Type: text/html; charset=UTF-8
Date: Mon, 24 Sep 2018 20:44:32 GMT
Server: LiteSpeed
Vary: Accept-Encoding
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Host: example.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
[/QUOTE]

Can you make your code work for apache first? then I can run another test to see any difference between apache and LSWS.
 
Last edited by a moderator:
#5
Hi,

Thank you for responding.


1)
It is pretty common to use $_SERVER["SERVER_PROTOCOL"] ... But before supplying an alternative code snippet I will move on to #2

2)
I have become aware of the fault in my own files (not code as such)

it is an issue the Apache servers I have used in the past apparently correct - but that has been dumb luck on my side it seems.

Problem: My text-editor used "Encode with UTF8" which includes a leading UTF8 BOM sequence... before first visible character "<?php" in the text editor. And that... renders PHP header() useless. My own fault - and I should have thought of it.

Before you ask: I did check request/response in some browser dev-tools to specificly look at if something wrong was transferred, but that did not show the UTF8 BOM bytes - at least not where I looked - so I had "happily" assumed that part was correct... Next time WireShark
 
Top