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 //set the 301 header header("HTTP/1.1 301 Moved Permanently"); //Set the url location to the redirected page header("Location: http://redirected-page-url.com"); ?>
<?php //This will result to 302 : Moved Temporarily redirect (should be avoided) header("Location: http://redirected-page-url.com"); ?>
If you read Google's Webmaster Guidelines you would see that they recommend to use 301 redirects instead of 302.
Code2care'smission 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.