What are the best practices for passing old URLs to new ones in PHP?
When migrating a website or changing URL structures, it is important to implement proper redirection from old URLs to new ones to maintain SEO rankings and ensure a seamless user experience. One way to achieve this in PHP is by using the header function to send a 301 (permanent) redirect header to the new URL. This tells search engines and browsers that the old URL has permanently moved to the new one.
<?php
$oldUrl = "http://example.com/old-page";
$newUrl = "http://example.com/new-page";
header("HTTP/1.1 301 Moved Permanently");
header("Location: $newUrl");
exit();
?>
Related Questions
- Is it better to create a new function or integrate functionality into an existing one when iterating through multiple database entries in PHP?
- In the provided code snippet, what improvements can be made to handle exceptions and errors more effectively?
- What are the common pitfalls to avoid when writing PHP code to handle form submissions and database interactions?