0

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?

FluffyKitten
  • 13,824
  • 10
  • 39
  • 52
DZ Flame
  • 3
  • 1
  • 1. Your code is not complete. `http://www.google.com/", true, 301); ?>` is "broken" and lacks first symbols. 2. Please, paste full reproducable example. – Arnis Juraga Jun 11 '20 at 23:40
  • @ArnisJuraga The code is correct - it was just not formatted correctly in the editor. It is also complete - this is all that is needed to explain the problem. – FluffyKitten Jun 12 '20 at 00:54
  • Then check My answer below. It's either one of two - try with additional url parameters to avoid browser caching, or double check php code for correct header redirection. Or there is something in between. PHP code is correct and there is nothing You can do to 'fix' what's not broken. – Arnis Juraga Jun 12 '20 at 00:57
  • @DZFlame This is most likely to do with the browser cache. The `301` told your browser it was a permanent redirect, so it has cached it until told otherwise. This has been asked and answered previously on StackOverflow, check those out. Also, in future use `302` for testing! A 302 redirect is temporary. – FluffyKitten Jun 12 '20 at 01:01
  • 1
    Does this answer your question? [Cannot remove 301 redirect](https://stackoverflow.com/questions/9431164/cannot-remove-301-redirect) – FluffyKitten Jun 12 '20 at 01:01

1 Answers1

0

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.

Arnis Juraga
  • 1,027
  • 14
  • 31