【发布时间】:2012-06-10 05:48:35
【问题描述】:
如何在链接到内部页面时将用户重定向到外部站点?
我见过这样的例子:
- example.com/go/ksdjfksjdhfls
- example.com/?go=http://www.new-example.com
- ...还有更多...
这在php中是如何实现的?
这对 SEO 有什么优点/缺点吗?
【问题讨论】:
如何在链接到内部页面时将用户重定向到外部站点?
我见过这样的例子:
这在php中是如何实现的?
这对 SEO 有什么优点/缺点吗?
【问题讨论】:
我认为这种方法没有任何好处,但有几种方法可以实现。要使用 GET 查询,您只需要以下代码:
<a href="http://example.com/link.php?site=http://www.google.com">Google!</a>
if (filter_var($_GET['site'], FILTER_VALIDATE_URL)) {
header('Location: ' . $_GET['site']);
}
在上面的例子中,它实际上会将用户带到那个位置,而不是:
http://example.com/link.php?site=http://www.google.com
要在拉取远程站点时使 url 成为“本地”,您必须:
所以在我能想到的三种服务器端方法中,一种可能可行,也可能不可行,而且很痛苦。一个会瘫痪并给服务器带来沉重的负担。最后一个是众所周知的坏人,可能在很多情况下都不起作用。
所以我只需要重定向,实际上,如果您不需要地址栏来显示本地 URL,那么我只需要一个直接链接。
所有这些都提出了一个问题:您希望完成什么?
【讨论】:
把它放在任何输出到浏览器之前开始
<?
header('location:example.com\index.php');
?>
【讨论】:
设置一个索引php文件,将标头位置设置为get参数中的url。
example.com/?go=http://www.new-example.com :
// example.com/index.php
<?php
if (isset($_GET['go'])) {
$go = $_GET['go'];
header('Location: $go');
} // else if other commands
// else (no command) load regular page
?>
example.com/go/ksdjfksjdhfls :
// example.com/go/ksdjfksjdhfls/index.php
<?php
header('Location: http://someurl.com');
?>
【讨论】:
example.com/?go=http://www.new-example.com
您可以使用 iframe 并将 src 属性设置为http://www.new-example.com
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<iframe src="http://www.new-example.com" width="100%" height="100%"></iframe>
</body>
</html>
【讨论】:
我为此使用 .htaccess 规则。无需 PHP。
即
Redirect 307 /go/somewhere-else http://www.my-affiliate-link.com/
所以访问http://www.mywebsite.com/go/somewhere-else 将重定向到http://www.my-affiliate-link.com/。
在我的网站上,我使用“nofollow”来告诉搜索引擎不要跟随链接。 307 状态码表示“临时重定向”。
<a href="http://www.mywebsite.com/go/somewhere-else" rel="nofollow">Click here!</a>
【讨论】: