301 Permanent Moves from a Sub-directory to another server when Redirect from Apache won’t work
I had a problem the other day where I had to move my server to a new server location… In my case I was moving the support pages for my Wordpress Plugin “Share and Follow” from my Phat-Reaction server to a new server devoted to Share and Follow.
To make it a little more difficult I need to only move a sub-directory to the new server and not the whole domain…. this meant that I was moving
Source: http://phat-reaction.com/share-and-follow/ (and all sub-directory contents)
to
Destination: http://share-and-follow.com/wordpress-plugin/
At the source there were many pages below the /share-and-follow/ subdirectory, all delivered via Wordpress on Apache. In fact the pages used up to 3 levels of sub-directory below this point.
So I tried to use the Apache Redirect command inside both <Directory> and <VirtualServer> tags but to no avail. In fact when I contacted my hosting company they told me point blank that it could not be done with their server and that 301 Redirect’s could not work when going to an external server. Not sure I believe that, but no matter what I tried I could not make the standard Apache method work.
To make it work I chose to use PHP and header() commands along with a few extra little bits. Here is the process I took to move it all
- Changed the URL inside the Wordpress settings to the new URL
- Made a back up of the Database
- Placed the new .htaccess in the sub-directory
- Placed the new index.php in the sub-directory
- Deleted all traces of Wordpress from the directory and sub-directories
In essence what it does is use Mod-ReWrite to tell the server that all access to the server below the /share-and-follow/ directory should actually load /share-and-follow/index.php. So it did not matter if it was /share-and-follow/documentation/working-with-themes/ or /share-and-follow/my-stats.html it would always load the same index.php. this is all controlled by the .htaccess file.
example .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /share-and-follow
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /share-and-follow/index.php [L]
</IfModule>
remember to change the settings to be as you need for your server. For me I was working in the share-and-follow sub-directory. The only lines that need to be changed for your purpose is RewriteBase and RewriteRule. RewriteBase should point at the sub-directory that you want to move, RewriteRule should point at the file you want to automatically load to deal with the redirection.
Once the URL rewrite is in place, all that is left is to configure the php side of things.
example index.php
<?php
/*
* This file simply redirects with a 301 to a new page with the same name when used in conjunction with a .htaccess
*
* @param $stubOld = the Stub URI that will be replaced with the new URL
* @param $stubNew = the new URI location for the files.
*/
$stubOld = 'http://phat-reaction.com/share-and-follow/';
$stubNew = 'http://share-and-follow.com/wordpress-plugin/';
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$url = curPageURL();
$url = str_ireplace($stubOld ,'' , $url);
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$stubNew.$url);
flush();
?>
In this one you only need to change two things. The $stubOld = Old Source URL and $stubNew= New Destination URL. By changing these two items a URL such as http://phat-reaction.com/share-and-follow/documentation becomes http://share-and-follow.com/wordpress-plugin/documentation
And the great part is that Google loves it when you do things this way with a 301 error (Moved Permanently).
Enjoy and share it with your webmaster buddies. Here are the files if you want to download them
