PHP redirect header

#PHP #redirect #header

Sometimes, a webmaster may notice that a file has the wrong name or that the organization of the directories must be changed. While it is relatively simple to change local links, changing those directed to other sites can be more complex. On the other hand, when pages change location, search engines can display the known 404 error or, even worse, they deindex the page. Luckily, there is a simple solution to this problem: the link redirection, which allows a website to redirect content to another domain or URL, maintaining the page’s navigation logic even if all files move.

  • HTTP headers

  • Simple redirect

  • Relative or absolute route

  • Temporary / permanent redirects

HTTP headers

Redirects are HTTP headers. According to the HTTP protocol, HTTP headers must be sent before any other type of content, which means that no characters must be sent before the function call header not even a space. In other words, the function header () must be used before any HTML code.

Simple redirect

To redirect the visitor to another page using a PHP script, we will use the element header (). Enter the address of the page to which you want to redirect. This address can be absolute or have settings like¶m2=val2.

It is very important to take into account that the location header must be indicated before displaying any information on the screen (HTML code, blanks). The PHP code located after the function header () will be interpreted by the server even if the visitor is already at the address specified in the target, which means that, in most cases, you will want to place the function exit () to not generate work to the server unnecessarily.

Relative or absolute route

When making the indication, you must pay attention if the route to be mentioned is of the absolute or relative type. If we use the element header (), remember that for some browsers, the URL you want to redirect cannot be relative.

The route absolute contains the full domain name (including the HTTP protocol) while the route relative is formed from the previous one, but does not require certain parts of the linked resource. The relative route is useful for saving time, but requires additional information to locate the desired resource.

Let’s look at the difference between them with two examples:

Temporary / permanent redirects

The three most used redirects are divided into temporary and permanent. Are they 301, 302 and 307. The 301 redirect is permanent, which means that the page has been irreversibly moved to another page. The deleted content is replaced with a new one. The following shows how to do this redirection with PHP:

header('Status: 301 Moved Permanently', false, 301);
header('Location: endereço_da_nova_página);

Redirects 302 and 307 are temporary. In this case, the source URL still exists, but it must be transferred to another location temporarily, with the intention of retrieving it at another time.

The difference between them is that the 307 indicates that the content has temporarily disappeared. Let’s look at the PHP code for temporary redirection:

header("HTTP/1.1 302 Moved Temporarily");;
header("Location: endereço_da_nova_página_temporária);

Photo: © kaspri –

Related Posts