With nginx/php-fpm, location header sometimes ignored by browser. Why?

后端 未结 1 819
我在风中等你
我在风中等你 2021-01-16 08:05

I\'m in the process of moving a working apache/mod-php website to nginx/php-fpm.

Under Apache/mod-php, I could use header(\"Location: $url\"); to redire

相关标签:
1条回答
  • 2021-01-16 08:29

    The Location header does not, by itself, trigger the browser to redirect. The redirect is actually triggered by an HTTP response code that is in the 3xx series. w3c has explanations for all http response codes.

    Apache automatically sees the Location header in the response, and forces the response code to be 300-series if you haven't previously set a response code of your own. Nginx does not do this -- it expects you set the proper response code yourself.

    You can force php to send a modified HTTP response code like this:

    <?php
      header("HTTP/1.0 301 Moved Permanently");
    ?>
    

    ...Then, of course, you'll need to be sure you still sent your Location header after sending the HTTP/1.0... line shown above.

    0 讨论(0)
提交回复
热议问题