一句代码用PHP实现301永久重定向

2020-12-19 18:12:51
用一句代码,PHP就能实现HTTP 301永久重定向,代码如下 ```php <?php header('Location: https://www.example.com/', true, 301); ``` 验证一下 ```shell $ curl -i http://localhost/301.php HTTP/1.1 301 Moved Permanently Content-Type: text/html; charset=UTF-8 Location: https://www.example.com/ ``` 网上很多贴子中的代码,验证无效,例如 ```php <?php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.example.com/"); ``` 验证发现,状态码并不是301,而是302 ```shell $ curl -i http://localhost/301.php HTTP/1.1 302 Found Content-Type: text/html; charset=UTF-8 Location: https://www.example.com/ ``` 不要迷信网络,学一学HTTP协议,自己验证一下