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
- Delete file using PHP code : unlink()
- PHP header location function not called
- 403 forbidden error for Image
- Call PHP function on Button click using jquery ajax
- How to Pretty Print JSON in PHP
- Step-by-Step Guide: How to Fix - Error Establishing a Database Connection in WordPress
- PHP Base64 encoding decoding a variable
- PHP Fatal error : Call to a member function bind_param() on a non-object
- PHP.ini: How to Remove URL Forward Slash Before Single or Double Quotes
- PHP drag and drop file upload tutorial using dropzone.js
- Upload Pdf file using PHP Script
- PHP Warning: Cannot modify header information - headers already sent
- macOS - cannot calculate MAC address: Using fd 9 hv_vm_create HV_ERROR [PHP XAMPP]
- PHP Code for sending Emails
- How to destroy PHP session()
- Installing vue.js in Laravel 8
- PHP 301 Redirect Permanently
- Upload docx file using PHP script
- Save current timestamp in MySQL using PHP mysqi binding
- Copy file from one directory to other in Php
- Failed to load resource: net::ERR_CACHE_MISS PHP
- PHP Script to Upload Images to Server
- cURL Option to show HTTP Response Status Code - cURL
- How to Find Where Mac Terminal App is Located? - MacOS
- Download Google Chrome setup exe file using PowerShell - Powershell
- How to install pip on macOS using terminal command [Python] - Python
- Blank Operations in Notepad++ - NotepadPlusPlus
- Spring Boot: Transactions Management with JDBCTemplate Example - Java
- How to locate Python Installation on Windows (10/11) - Windows
- Show Notepad++ tabs in multiple lines instead of scroll on tab bar - NotepadPlusPlus