Program PHP redirection or PHP redirection

Icon PHP help

Icon PHP helpPHP offers the possibility to define redirects and redirects to other URLs with the command "header ()". You can actually only pass the URL to which the forwarding should take place as a parameter. To make the whole thing a little more transparent, I have a few examples from practice.

Please do not forget: in front of the code in the text file a " "which I leave out here for the sake of clarity ... Also important: There must be NO output in the browser before the call. That means before the"

Simple PHP redirect

header ('Location:'); exit ();

301 redirect with PHP

If you want to create a google-friendly redirect, for example to redirect subpages that have been renamed to the new address, then you put the status code 301 in front of it, which tells the Google bot that it is a permanent redirection. Otherwise PHP will automatically send a 302 header code stating that it is only a temporary redirect.

header ("HTTP / 1.1 301 Moved Permanently"); header ('Location:'); exit ();

Recommendation for most cases

As a rule, I use the SEO-friendly 301 redirect, which makes it clear to the search engines that the page you are surfing can be found permanently under a new URL. In practice, this often happens when the URL structure or file names change. And of course there is no short-term change here, but a permanent one.


Did you like the article and did the instructions on the blog help you? Then I would be happy if you the blog via a Steady Membership or at Patreon would support.

Leave a Comment

Your e-mail address will not be published.