I'm looking for a detailed explanation of PHP redirect vs htaccess redirect. Question is already asked here but my scenario is little different than that.
I have an old site lets say www.mydomain.com/oldsite/ And my new site is www.mydomain.com/newsite/
This is a wordpress installation on /oldsite and there are so many posts and writing all of them in htacccess is very lengthy process so if I just put a php code in header.php of theme before any html output which will redirect page to new address is it a good practice or should I switch to htaccess. Code I'm using is
if (is_singular('post')){
$link = get_the_permalink();
$new_link = str_replace("oldsite","newsite",$link);
header("HTTP/1.1 301 Moved Permanently");
header("Location:".$new_link);
exit();
}
URL structure on /newsite is bit different than /oldsite and I'm handling all posts and archives redirection with php after appending the URL structure changes in string before php redirection.