homepage Welcome to WebmasterWorld Guest from 54.226.10.234
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
site mirroring with php & .htaccess
MSamu



 
Msg#: 4645154 posted 12:05 pm on Feb 14, 2014 (gmt 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.

 

penders

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



 
Msg#: 4645154 posted 8:35 pm on Feb 16, 2014 (gmt 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?)

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4645154 posted 1:23 am on Feb 17, 2014 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved