如何防止页面显示在iframe外部

时间:2014-08-15 10:41:18

标签: php html iframe

我有一些页面,遗憾地列在谷歌中。

我现在将这些页面作为iframe包含在标签页的其他页面中。

如何将登陆原始页面的任何人重定向到新位置。

所以page1.php现在是page_new.php#page1

原始页面已被删除所有页眉/页脚等,因此从它有限的导航。

3 个答案:

答案 0 :(得分:1)

感谢。

我也找到了这个

   <script type="text/javascript"> 
if(top.location == self.location) 
{ 
top.location.href = 'http://www.newurl.com/#map' 
} 
</script>

这似乎也有效。

非常感谢所有人:)

答案 1 :(得分:0)

你可以在推荐人的基础上进行重定向吗?

<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/site1.com/",$referrer)) {
      header('Location: http://www.customercare.com/page-site1.html');
} elseif (preg_match("/site2.com/",$referrer)) {
      header('Location: http://www.customercare.com/page-site2.html');
} else {
      header('Location: http://www.customercare.com/home-page.html');
};
?>

来源:http://www.phpjabbers.com/redirect-based-on-referrer-or-ip-address-php2.html

或者,如果未设置某个$ _GET变量,则可以在PHP中执行重定向。当您通过iFrame包含页面时,请始终进行设置。这样,直接到iframe页面的外部流量将被重定向到右侧页面。

顺便说一句,在iframe页面的标题内使用<meta name="robots" content="noindex, nofollow">,以便以后它不会显示在搜索引擎中。

答案 2 :(得分:0)

你无法在PHP中真正做到这一点,因为它正在检查客户端(无论是否在iFrame中)。

正如Maerlyn所指出的,JavaScript是你的朋友。该主题已多次涉及;这个例如: How to identify if a webpage is being loaded inside an iframe or directly into the browser window?

在那里考虑格雷格的建议并对其进行调整:

function inIframe () {
    try {
    return window.self !== window.top;
    } catch (e) {
    return true;
    }
}
if (inIframe() !== TRUE) {
    window.location = "http://example.com"
}

使用首选网页的网址替换example.com。

相关问题