Permanent 301 Redirect with PHP

On 4/17/2017

Hey! In today's post let's see how to do permanent 301 redirect with php. Redirection is a very common practice in websites and can be done at page or domain level. There are two types of redirect, one is temporary and other is permanent. The temporary redirect sends 302 http code with headers and indicates browsers and search engines that the page is moved temporarily. And permanent redirection sends 301 code and tells browsers and search engines that the page is moved permanently to different location.

You can do page redirects via htaccess file or within code. PHP has this header() function which lets you redirect users from one page to another. No doubt most of you have used it countless times. But do you know the redirect you do is just temporary? Because unless stated explicitly the headers are sent for 302 redirect. So let's see how to perform 301 redirect in php here.

301 redirect in php

Permanent 301 Redirect with PHP:

Sometimes you might have moved a resource to another place and want to redirect the page permanently. It's also good for SEO and 301 redirect will pass link juice to the new url.

Also Read:

In php you have to redirect to another url like this,

header("Location: example/page.php");

Just pass the destination page to header() and it will attach 302 code and do temporary redirect. But you can also make the function to perform 301. For that you must state the response code with headers like this.

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://example.com/sample/page/");
?>

The above code will redirect users to the new url attaching a 301 http response code to headers. That's instructs browsers and bots to take it as permanent redirect. (Learn how to get http status code from Headers in PHP.)

If you think two lines of code is too much then refine it into a single line like this.

<?php header("Location: http://example.com/sample/page/", true, 301); ?>

The above line will permanently redirects to the provided page.

Most of us use only one param with in header() but the function takes up to three parameters.

  • First parameter is obviously the page location.
  • Second param is a boolean value and is useful if you send multiple headers of same type. True will replace same type header and False will force to send multiple headers of similar type.
  • Third param is the 'http_response_code' and attach the headers with redirection code.

Permanent 301 Redirect via HTACCESS:

Redirecting page via htaccess is even easier and practiced by so many webmasters. To redirect a page permanently add this line to the htaccess file in server.

redirect 301 /old-file.php /new-file.php

The above code will permanently redirect old-file.php to new-file.php with in the same domain.

If you have to redirect a file to another domain then specify the entire url.
redirect 301 /old-file.php http://another-domain.com/another-file.php

Likewise you can perform permanent 301 redirect in php. Make sure to place headers before actual output starts in php file. Else you will get error. I hope you find this useful. Meet you in next tutorial:D

No comments:

Post a Comment

Contact Form

Name

Email *

Message *