通过重定向(301),我写入http头的内容在PHP中消失了

In an endpoint of my web service, I am adding some line to http response header and then it is redirected to another url in the service. This is 301 redirection and the header I wrote is removed somehow when this 301 response is sent to the browser.

I am calling header function in PHP to write to header and then calling redirect for 301 redirect. Is there any step I missed? Here is a rough flow:

header("X-my_custom_header:blahblah");
redirect($new_url);

redirect function is calling 3 header functions:

header("HTTP/1.1 301 Moved Permanently");
header( "Cache-Control: no-cache");
header("Location: ".$new_url);

New Finding:

If I move the new header addition line inside redirection function (as 4th line in the function), then I can see the line.

Thanks in advance!

The 301 redirect triggers a new request from the browser. Accordingly a new response is created on the server and this new response does not have any of the headers generated as part of the 301 response.

If you need to persist some data, you will need to either store it in the session or pass it via GET parameters on the 301 URL.