How to change website URLs with 301 redirect (Moved Permanently)

April 03, 2014

If you need to change website URLs and don't want to loos all links and positions on search engines, you need to create new URLs and do 301 redirect (Moved Permanently). Here is how you can do this.

 

For this example old URL: /old-folder/some-url and new one: /new-folder/some-url

301 done is following way:

<?
header
("HTTP/1.1 301 Moved Permanently"); 
header("Location: /new-folder/some-url");
?>

If you need to insert it into the file only when someone is trying to access old URL.

First of all you make sure that old URLs are pointed to the same file as new once and add following code:

<?
$querryStrArr
=explode("/",$_SERVER['REQUEST_URI']);
if(
$querryStrArr[1]=="old-folder ")
{            
    
header("HTTP/1.1 301 Moved Permanently"); 
    
header("Location: /new-folder/".$ querryStrArr[2]);
    exit();
}
?>