简体   繁体   English

从一个站点重定向到另一个站点

[英]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 1.com
  2. 2.com 2.com

2.com is my main website. 2.com是我的主要网站。 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. 当我将页面添加到2.com(例如:2.com/index.html)时,1.com会忽略该页面,并使用重定向代码创建到2.com/index.html来创建(1.com/index.html)。 Can I do this ? 我可以这样做吗?

Is there any way to do this by php ? 有什么办法可以用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? 那么我可以用php和mysql或任何其他脚本或编程语言来做到这一点吗?

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 . 如果您同时拥有两个域,则可以使用DNS-A记录或其他任何方法将它们都重定向到您的网站,然后只需使用apache.org上概述的服务器别名( Server Alias )即可。 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. 然后,您的用户将始终在其地址栏中看到1.com。

// Put this script on 1.com and it will redirect to to 2.com or vice versa //将此脚本放在1.com上,它将重定向到2.com,反之亦然

<?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 //超级hack时间

<?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. 再次,我真的不知道为什么要这么做。

Impossible to do with PHP, since a PHP code is executed when file is launched, and not when any file on server is launched. 与PHP无关,因为PHP代码是在文件启动时执行的,而不是在服务器上的任何文件启动时执行的。 Possible with .htaccess : 可能与.htaccess

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

Redirecting to www.newdomain.com from every page on your old domain. 从旧域的每个页面重定向到www.newdomain.com。


See this post for more methods about redirecting. 有关重定向的更多方法,请参见这篇文章

The above answer can only be used before any html has been loaded. 以上答案只能在加载任何html之前使用。 If you're looking for something that is easier to implement use this: 如果您正在寻找更易于实现的方法,请使用以下命令:

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

I'm not sure if I completely understood your question. 我不确定我是否完全理解您的问题。

With PHP 使用PHP

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

With HTML 使用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 将CNAME记录添加到1.com的DNS中,值为2.com

I would prefer to setup Nginx web server on 1.com and configure it as a proxy, so 2.com actually handles all requests. 我希望在1.com上设置Nginx Web服务器并将其配置为代理,因此2.com实际上会处理所有请求。 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. 因此,您可以避免在1.com上复制整个2.com,并且同时不会像使用Location标头那样将用户浏览器重定向到1.com。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM