[PHP] Automatical redirect

Dieser Beitrag ist auch verfügbar in: German

Hi,

today I would like to talk quick about the subject “Redirection with PHP”. Quick, because there is not much to say :)

The code for a redirect in PHP looks like that:

<?php
	header("Location: http://www.domain.de/seite.php"); 
	exit(); 
?>

header() is sending the HTML-header. With “Location:” you’re telling it that it is an redirect. After it you provide the URL to which the redirect it pointing to. And with exit(); we tell the script to abort, not to execute any other code.

But I also have to mention to thing about what you do before you set an redirect. Is it a permanent redirect, because something changed in the webpage structure (for example a page got a new URL), then you should “say” that in the redirect, else it can occur problems with the search engine rankings.

A permanent redirect looks then like that:

<?php
	header("Status: 301 Moved Permanently");
	header("Location: http://www.domain.de/seite.php"); 
	exit(); 
?>

The line header("Status: 301 Moved Permanently"); is sending the status code that the redirect is permanent. There are also other status codes, like the 302 code, which is for temporarily redirects.
If e.g. the Google bot comes around and gets an 301 status code for a URL, then it will save the URL change and is crawling next time straight the new page. Like that the new page gets the ranking of the old page and it doesn’t get lost.

The good thing on PHP redirection is that it happened on the server side and will always work. On the other side you can make it dynamically. Redirection with the .htaccess can be also dynamic, but not as good as with PHP. Redirects in the HTML-Header are totally underground. The same for Javascript redirects. They doesn’t work at all for spider and user without Javascript.

I hope it’s helping. If not, let me have a comment :)

Best regards
Gordon

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy