Ian
Ian

Reputation: 157

How to prevent a page from showing outside of an iframe

I have some pages, which regretfully are listed in google.

I am now including those pages in other pages within a tab as an iframe.

How can I redirect anyone landing on the original pages to the new location.

So page1.php is now page_new.php#page1

The original page has been stripped of all headers/footers etc., so limited navigation from it.

Upvotes: 3

Views: 1754

Answers (3)

Ian
Ian

Reputation: 157

Thanks.

I have also found this

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

Which seems to also work.

So thanks all :)

Upvotes: 1

Geoff Atkins
Geoff Atkins

Reputation: 1703

You can't really do this in PHP as it's something on the client side (whether it is in the iFrame or not) that you are checking.

JavaScript is your friend here, as Maerlyn pointed out. This topic has been covered several times; this one for example: How to identify if a webpage is being loaded inside an iframe or directly into the browser window?

Taking Greg's suggestion there and adapting it thus:

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

Replacing example.com with the URL of your preferred page.

Upvotes: 0

chocolata
chocolata

Reputation: 3338

Could you do a redirect on the basis of referrer?

<?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');
};
?>

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

Alternatively, you could do a redirect in PHP if a certain $_GET variable is NOT set. When you include the page via iFrame, always set it. This way, outside traffic directly to the iframe-page would be redirected to the right page.

By the way, use <meta name="robots" content="noindex, nofollow"> inside the header of the iframe page, so that in future it will not show up in search engines.

Upvotes: 0

Related Questions