Is it possible to set some http headers while http

2019-06-23 17:14发布

问题:

Is it possible to set some http headers while http-redirect(302 or 307)?

<?
 header("some-header: xxx");
 header("Location: http://other.domain.com/foo.php",TRUE,307);

?>

回答1:

You can basically set whatever http headers you want either as the server or the client.

If you are indicating a redirect you should supply the Location header as your example suggests. You should also ensure that your response headers refer to that response rather than the resource that the client is being redirected to. i.e. your headers here could include Content-Length: 0, omit the Content-Type header and so on.

Not sure if this is what you're after - this question could do with a bit more detail.



回答2:

You can always do the redirection 301/307. There are ways to do it 1) Do it through java code :

response.setStatus(307);
response.setHeader("Location",url);

2) THe same thing can be done in JSPs.

A tip here is: Always use the setHeader function and not the addHeader function as they behave in different ways.