PHP 301 Redirect Permanently


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 a very important part of SEO (Search Engine Optimization) that your redirect is 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. Let's see a code snippet for the same,

PHP 301 Redirect Code :
<?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"); 
?>

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.

<?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 using 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."

- https://support.google.com/webmasters/answer/93633?hl=en

Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency