I was trying to write a 301 redirect and used the following code:
<?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.
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. 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?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. You must exit or die
after the correct one.
<?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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.