Category: Web Server

301 Permanent Moves from a Sub-directory to another server when Redirect from Apache won’t work

20th May 2010 by andyk

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

  1. Changed the URL inside the WordPress settings to the new URL
  2. Made a back up of the Database
  3. Placed the new .htaccess in the sub-directory
  4. Placed the new index.php in  the sub-directory
  5. 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

Want to be able to play video content on your PS3 without getting DLNA errors?

24th November 2009 by Phat Reaction

Being the techie fool that I am, it was a breeze for me to setup my latop with a massive external hard disk that manages all of my video content.  The only problem I found almost instantly was that playstation3 uses DLNA to talk between the media server and the it.  DLNA is crappy at the best of times and when used on my playstation in tune with TVersity I found it was all kinds of  wrong, loosing audio and video all over the place and often crashing.

Check out the full instructions I made with you tube videos on how to do it.  It even covers converting from most formats into a playstation playable one.

Here’s a quick process overview