Title: How to use PHP to implement 301 redirection
In the process of website development, we often encounter situations where page redirection is required. Redirect is to automatically direct users from one URL address to another URL address. 301 redirect is the most commonly used one. It tells search engines and browsers that the requested resource has been permanently moved to another location.
In PHP, implementing 301 redirect is very simple. You only need to use the header() function to send a specific HTTP response header to implement a 301 redirect.
The following is a simple sample code that demonstrates how to use PHP to implement 301 redirection:
<?php // 设置要重定向的目标URL $redirect_url = 'https://www.example.com/new-page'; // 发送301重定向标头 header("HTTP/1.1 301 Moved Permanently"); header("Location: $redirect_url"); exit(); ?>
In the above code, the target URL address to be redirected is first defined, and then header( ) function to send HTTP response headers: HTTP/1.1 301 Moved Permanently means that the resource has been permanently moved, and the Location header specifies the new target URL address. Finally, use the exit() function to terminate the execution of the script.
In actual applications, you can extend and modify the code as needed, such as determining the target URL for redirection based on different conditions, or performing some operations before redirection, etc.
In general, implementing 301 redirection in PHP is very simple and only requires a few lines of code to complete. By using 301 redirects appropriately, you can help users and search engines better handle page movement or deletion, and improve the user experience and SEO effects of your website.
The above is the detailed content of How to implement 301 redirect using PHP. For more information, please follow other related articles on the PHP Chinese website!