Wednesday, 15 January 2014

Redirecting a website to another website without changing the url

In this post Iam explaining how to redirect a website to another website without changing it's url.
For eg: If you have a domain named adidas.com. You want to redirect every page request of adidas.com  to nike.com.
Like adidas.com/aboutus.php should show the contents of nike.com/aboutus.php , but the url will remain as adidas.com/aboutus.php.

So Now I will explain how to acheive this. This can be acheived by CURL function in php.
Before doing this please ensure that you have access to both domains.& curl option is enabled in your server.

Here I will explain with an example.

First make a file named .htaccess

RewriteEngine On
RewriteRule index.html  index.php
RewriteRule Aboutus.html about.php
RewriteRule ContactUs.html  contact.php



Place above code in that .htaccess file.
Then create a file named index.php

<?php
getsite("targetsite/index.php");// Here give the target page from where to show the content

function getsite($url){
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// grab URL and pass it to the browser
$urlContent = curl_exec($ch);
$excont=str_replace('targetsite','yoursite',$urlContent);// Here replace the menu link with your sites

// Check if any error occured
if(!curl_errno($ch))
{
   $info = curl_getinfo($ch);
   header('Content-type: '.$info['content_type']);
   echo $excont;
}


// close cURL resource, and free up system resources
curl_close($ch);

}
?>

You have to create about.php,contact.php  similar to the above file by just changing the parameter in getsite function.
Hope it works.

No comments:

Post a Comment