Welcome to WebmasterWorld Guest from 54.198.229.157

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

site mirroring with php & .htaccess

     
12:05 pm on Feb 14, 2014 (gmt 0)

New User

joined:Feb 10, 2014
posts:5
votes: 0


nb: I don't expect anyone to do this for me. However I certainly won't refuse the offer.


OK,
I got a new domain and I'm trying to mirror my old domain on it. I've found a script on line and it more or less works BUT...

The graphics and layout are all messed up or not displaying.

Here's the script
------------------------------
# index.php
<?php
// enter your site names here. Just the core url no http or tail slashes
$wc_source = "The old site .com";
$wc_mirror = "the new domain.com";
/*
Look hard under this line. But dont try to change unless you are above 18 and know how to read awasthakiyoguyiomonstia
*/
$sql = $_GET['sql'];
$URL = "http://".$wc_source."/".$sql;
$fullPath = $URL;
$base = '<base href="'.$URL.'">';
$host = preg_replace('/^[^\/]+\/\//','',$URL);
$tarray = explode('/',$host);
$host = array_shift($tarray);
$URI = '/' . implode('/',$tarray);
$content = '';
$fp = @fsockopen($host,80,$errno,$errstr,30);
if(!$fp) { echo "Unable to open socked: $errstr ($errno)\n"; exit; }
fwrite($fp,"GET $URI HTTP/1.0\r\n");
fwrite($fp,"Host: $host\r\n");
if( isset($_SERVER["HTTP_USER_AGENT"]) ) { fwrite($fp,'User-Agent: '.$_SERVER["HTTP_USER_AGENT"]."\r\n"); }
fwrite($fp,"Connection: Close\r\n");
fwrite($fp,"\r\n");
while (!feof($fp)) { $content .= fgets($fp, 128); }
fclose($fp);
if( strpos($content,"\r\n") > 0 ) { $eolchar = "\r\n"; }
else { $eolchar = "\n"; }
$eolpos = strpos($content,"$eolchar$eolchar");
$content = substr($content,($eolpos + strlen("$eolchar$eolchar")));
$content = str_replace($wc_source,$wc_mirror,$content);
// replaces paths with / in the begining without full url. not required unless you are in subfolder
$content = str_replace('href="/','href="http://'.$wc_mirror.'/',$content);
$content = str_replace('src="/','src="http://'.$wc_mirror.'/',$content);

// $fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);

// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg": $ctype="image/jpg"; break;
case "jpg": $ctype="image/jpg"; break;
case "js": $ctype="text/javascript"; break;
case "css": $ctype="text/css"; break;
}

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Transfer-Encoding: binary");
// header("Content-Length: ".$fsize);


if( preg_match('/<head\s*>/i',$content) ) { echo( preg_replace('/<head\s*>/i','<head>'.$base,$content,1) ); }
else { echo ( str_replace($wc_source,$wc_mirror,preg_replace('/<([a-z])([^>]+)>/i',"<\\1\\2>".$base,$content,1) ));
}
?>
--------------------------

** Here's the .htaccess
#htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?sql=$1 [L]
</IfModule>

--------------------------



Please forgive me for doing a lot of cut and paste. I've done several php tutorials but it's awful hard and slow going for me to understand. It's not a wimp out, I'm just more of an artist instead of a programmer.
8:35 pm on Feb 16, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


I got a new domain and I'm trying to mirror my old domain on it.


Really? Any reason you aren't simply redirecting? (Or setting up the old domain as an add-on domain, and redirecting?)
1:23 am on Feb 17, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


The graphics and layout are all messed up or not displaying.

99 times out of 100 this means that the html is looking for the non-html in the wrong place. Most of the time this in turn is due to an improperly setup rewrite (which could include a full-blown proxy).

It may be helpful to set aside the code and simply lay out in English what's currently happenening vs. what you want to have happen.