An Alternative proxy script to host your WordprssBlog anywhere else


Months ago renowend PHP programmer Hasin Hayder posted a nifty thing of only 10 line proxy. Now this code had been written by Ferdous just after that post had been made. I am using this code to host my blog temporarily. But I felt the need for blogging about it which might help many programmers to better understand the technics. Definitely Hasin bhai’s technic is on the top. But we had a thought in mind that Regular Expressions are a bit costly and on a Conversation with him on Gtalk he said if you can make an alternative to this not using regular expression I am gonna treat you :P  So, you know this is done for the challenge as well.😀 Comparing the time of execution it yeilded a bit slower than Hasin bhai’s script. I have added the main proxy syntax highlighted here.

Remember, you need the .htaccess file with this to make it work. Go get the code from Hasin bhai’s original post🙂

< ?php
    error_reporting(0);
    $primary_url="https://lenin9l.wordpress.com/"; //original url of your wordpress blog
    $secondary_url='http://savvycoders.com/'; //here the new hosted url should be specified
    $path =array_keys($_GET);
    $data_url = (!empty($path[0]))?$primary_url."/".$path[0]:$primary_url;
    $doc=new domdocument('1.0','UTF-8');
    $doc->loadhtmlfile($data_url);
    $data = $doc->saveHTML();
    $tags=$doc->getElementsByTagName('a');
    foreach($tags as $tag){
        $href = $tag->getAttribute('href');
        $href_new = str_replace($primary_url, "$secondary_url", $href);
        $data = str_replace($href,$href_new,$data);
    }

    $form_tags=$doc->getElementsByTagName('form');
    foreach($form_tags as $tag){
        $action = $tag->getAttribute('action');
        $action_new = str_replace($primary_url, "$secondary_url", $action);
        $data = str_replace($action,$action_new,$data);
    }
    echo $data;
?>

Now Your .htaccess file need to have the following codes:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.+)
RewriteRule ^(.*)$ index.php?$1&%{QUERY_STRING}


RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

3 thoughts on “An Alternative proxy script to host your WordprssBlog anywhere else

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s