Web Development Blog

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

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();
}
?>

Associated tags:  URL, 301, Moved Permanently, 301 Redirect, Redirect

Comments:

Davids wrote on April 3, 2014 at 19:00
Thanks for the help. Works fine. How do I check if 301 is done properly?

Michael Pankratovs wrote on April 7, 2014 at 12:58
Good question.
We created a tool that allows you to check headers. Here is a link to: Redirect Check Tool

Add Comment:

CAPTCHA