简体   繁体   English

php 301 重定向到错误的链接

[英]php 301 redirecting to the wrong link

I was trying to write a 301 redirect and used the following code:我试图编写一个 301 重定向并使用以下代码:

<?php header("Location: http://www.google.com/", true, 301); ?>

This was to test if the redirect worked at all, and when it did I changed it to my eventual end link.这是为了测试重定向是否有效,以及何时将其更改为最终的结束链接。 However, it kept redirecting to Google even after the change.但是,即使在更改之后,它仍然会重定向到 Google。

I have tried clearing browser cache, using different browsers, and have restarted both the computer that I am working on it with, and the server that the code is running on.我尝试使用不同的浏览器清除浏览器缓存,并重新启动了我正在使用的计算机以及运行代码的服务器。

What else can I do to clear the old redirect?我还能做些什么来清除旧的重定向?

To avoid cached redirects, try to open the same page with new url parameters.为避免缓存重定向,请尝试使用新的 url 参数打开同一页面。 If your test page has address https://www.example.com/rtest.php , to test redirect - every next time try to open address with:如果您的测试页面有地址https://www.example.com/rtest.php ,测试重定向 - 每次尝试打开地址:

https://www.example.com/rtest.php?a
https://www.example.com/rtest.php?b
https://www.example.com/rtest.php?c...

and so on.等等。 This excludes browser cached redirection and each time checks the server again!这不包括浏览器缓存的重定向,并且每次都会再次检查服务器!

But keep in mind - PHP code executes even after first "header" sets.但请记住 - PHP 代码即使在第一个“标题”设置之后也会执行。 You must exit or die after the correct one.在正确的一个之后,您必须退出或die

<?php 
header('Location: https://www.google.com', true, 301);
header('Location: https://www.bing.com', true, 301); ?>

Will be redirected to https://www.bing.com , as this is last header in code.将被重定向到https://www.bing.com ,因为这是代码中的最后一个 header。

If You have multiple redirects, only last one will work.如果您有多个重定向,则只有最后一个有效。

Set 'die' or 'exit' after exact one.在确切的一个之后设置“死”或“退出”。

<?php 
header('Location: https://www.google.com', true, 301);
exit; 
header('Location: https://www.bing.com', true, 301); ?>

This examle will redirect to Google.此示例将重定向到 Google。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM