gokul
gokul

Reputation: 305

redirecting from one site to another

Is there any way to redirect every pages on a website to another website ?

Actually what I mean is that, I own two websites eg :

  1. 1.com
  2. 2.com

2.com is my main website. When I add a page to 2.com (eg:2.com/index.html), 1.com ignores it and creates (1.com/index.html) with the redirecting code to 2.com/index.html. Can I do this ?

Is there any way to do this by php ?

Actually what I need is a script that automatically create files which are added to my 2nd site on my 1st site. So Can I do this with php and mysql or any other scripting or programming language?

Upvotes: 0

Views: 334

Answers (6)

Peter M. Elias
Peter M. Elias

Reputation: 1194

// Put this script on 1.com and it will redirect to to 2.com or vice versa

<?php

header('Location: http://2.com/index.html');
exit();

?>

If I did not understand your question correctly, let me know and I will help you as best I can.

// Super hack time

<?php
// 1.com
$files = scandir('./*'); // not recursive, note that
$sent = file($files['log.txt']);
unset($files['log.txt']);
$notsent = array_diff($files, $sent);
foreach($notsent as $file) {
     $contents = file_get_contents($file);
     // Use curl to post to 2.com receiving script http://davidwalsh.name/execute-http-post-php-curl
     file_put_contents($sent, $file, FILE_APPEND);
}
?>

Disclaimer: Have not tested, but it is the most direct way to do what I think you want. Again I really don't know why you would want to do this.

Upvotes: 2

skovalyov
skovalyov

Reputation: 2099

I would prefer to setup Nginx web server on 1.com and configure it as a proxy, so 2.com actually handles all requests. Thus you can avoid replicating the whole 2.com on 1.com and at the same time the user browser will not be redirected to 1.com like if you use Location header.

Upvotes: 0

John Wheal
John Wheal

Reputation: 10348

I'm not sure if I completely understood your question.

With PHP

header('Location: http://2.com');

With HTML

<meta http-equiv="refresh" content="2;url=http://2.com">

Having provided more information:

Add a CNAME record to the DNS of 1.com with the value of 2.com

Upvotes: 1

1&#39; OR 1 --
1&#39; OR 1 --

Reputation: 1714

If you own both domains you could just both redirect them to your website using a DNS-A-record or whatever and then simply use a server alias (Server Alias) as outlined on apache.org. If the user then visits the domain, he will still see the original domain, which he visited.

Another way would be using a rewrite rule as described by this blog:

RewriteCond %{HTTP_HOST} ^www.2.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^2.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.2.com$ [NC]
RewriteRule ^(.*)$ http://1.com/$1 [R=301,L]

Then your users would always see 1.com in their address bar.

Upvotes: 3

Nikola K.
Nikola K.

Reputation: 7155

Impossible to do with PHP, since a PHP code is executed when file is launched, and not when any file on server is launched. Possible with .htaccess:

RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

Redirecting to www.newdomain.com from every page on your old domain.


See this post for more methods about redirecting.

Upvotes: 2

Ilya Karnaukhov
Ilya Karnaukhov

Reputation: 3977

The above answer can only be used before any html has been loaded. If you're looking for something that is easier to implement use this:

<script>window.location = 'http://google.com';</script>

Upvotes: 1

Related Questions