Setting custom HTTP response headers in LiteSpeed with PHP

霸气de小男生 提交于 2019-12-12 01:45:59

问题


I'm trying to set a custom HTTP 1.0 status code in order to return more descriptive error messages via AJAX (using jQuery).

In PHP I have something like:

header( 'HTTP/1.0 909 Hello World' );
exit;

This works as expected on my development environment using Apache 2.2.15 but it does not work on the production webserver running LiteSpeed 5.5. LiteSpeed instead returns a 200 OK.

Using this:

header( 'HTTP/1.0 404 Hello World' );
exit;

Apache returns the 404 with the text above. LiteSpeed replaces the custom text with the default 'Not Found'.

Any idea how to override this feature in LiteSpeed to make it work the same way as Apache? I understand they are supposed to be comparable webservers.

Any ideas, please do let me know.

-P.


回答1:


First, why HTTP 1.0 and not 1.1?

Second, you're trying to do things that aren't defined in the HTTP spec. LiteSpeed probably checks the headers its being asked to send and replaces values it doesn't recognise with some default value.

You shouldn't be trying to force HTTP to behave in a non-standard way, as soon as you do, it ceases to be HTTP. The specifications are there to make sure that software implemented at different times by different people can interoperate, and how it should behave if you don't follow the spec is completely undefined.

Apache letting a header it doesn't know through is perfectly acceptable behaviour, and so is LiteSpeed's behaviour of refusing to let it through. Making demons fly out of your nose would also be a valid thing for the server to do in this case. And what is the client meant to do with an HTTP response code it doesn't recognise?

If you need to send additional information in the header, why not add a new header? The following should do.

header ('X-My-App-Header: Hello World!');


来源:https://stackoverflow.com/questions/10244528/setting-custom-http-response-headers-in-litespeed-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!