PHP 301 Redirect Permanently

Posted on : Fri, 24th Apr 15 09:01 am (Updated on : Fri, 24th Apr 15 09:01 am) UTC
Author : Code2care
100+ votes

There are times when you may want to redirect a webpage to a particular URL permanently, this is called a 301 redirect in HTTP request/response protocol. It is also very important part of SEO (Search Engine Optimization) that your redirect are correct. 301 redirects are called as Search Engine Friendly redirects. When the bots/crawlers read the url they know that the page has been permanently moved to a new location. Lets see a code snippet for the same,

PHP 301 Redirect Code :

File : redirect-301-page.php Language : Php
//set the 301 header
header("HTTP/1.1 301 Moved Permanently"); 

//Set the url location to the redirected page

Note : If you remove the first header line the page will be redirected to the specified new location but it would be a 302 Moved Temporarily kind of redirect and not 301.

File : redirect-302-page.php Language : Php

//This will result to 302 : Moved Temporarily redirect (should be avoided)

If you read Google's Webmaster Guidelines you would see that they recommend to use 301 redirects instead of 302.

"If you need to change the URL of a page as it is shown in search engine results, we recommend that you use a server-side 301 redirect. This is the best way to ensure that users and search engines are directed to the correct page. The 301 status code means that a page has permanently moved to a new location."


Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +