Skip to content
PHP Logo

What is the best way to redirect users from old URLs to new URLs using PHP scripts?

When you have changed the URLs of your website and want to redirect users from the old URLs to the new ones, the best way to do this using PHP is by using the header() function to send a 301 Moved Permanently header along with the new URL. This will inform search engines and browsers that the old URL has permanently moved to the new one.

php
<?php
$old_url = 'http://www.example.com/old-url';
$new_url = 'http://www.example.com/new-url';

header("HTTP/1.1 301 Moved Permanently");
header("Location: $new_url");
exit();
?>