Redirect a user to an external site while linking to an internal page?

How can I redirect a user to an external site while linking to an internal page ?

I have seen examples like:

  • example.com/go/ksdjfksjdhfls
  • example.com/?go=http://www.new-example.com
  • ... And many more...

How this is achieved in php ?

Does this have any pros/cons with regards to SEO ?

Answers


I don't see any benefit in this approach, but there are a few ways to achieve it. To do it with the GET query, you would simply need the following code:

HTML:
  <a href="http://example.com/link.php?site=http://www.google.com">Google!</a>
PHP:
if (filter_var($_GET['site'], FILTER_VALIDATE_URL)) {
          header('Location: ' . $_GET['site']);
}

With the above example, it will actually take the user to that location, not to:

 http://example.com/link.php?site=http://www.google.com

To achieve the url being "local" while pulling up a remote site, you'd either have to:

  • Mess with URL rewriting, which can get messy and I'm not sure will let you do the above
  • Retrieve the remote page via curl and display it, which may screw up links on the "remote" page
  • Use iframes and set the iframe to be the size of the page. Note that this last method, while least offensive, is recognized as a potential security breach known as 'clickjacking' since it's used to trick users into clicking on a link for one page which his hiding a malicious link to another site. Many servers and browsers are taking steps to avoid this (for instance, google does not allow iframing of its home page), so this may also reach dead ends.

So of the three server-side methods I can think up, one may or may not be possible, and is a pain. One will be crippled and put a heavy load on the server. The last is a known bad guy and is likely not to work for many cases.

So I'd just go with a redirect, and really, if you don't need the address bar to show the local URL, then I'd just have a direct link.

All of the raises the question: What are you hoping to accomplish?


Set up an index php file which sets the header location to the url in the get parameter.

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

you can use iframe and set the src attribute to 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>

put this is beginning before any output to browser

<?
header('location:example.com\index.php');
?>

I use .htaccess rules for this. No PHP needed.

i.e.

Redirect 307 /go/somewhere-else http://www.my-affiliate-link.com/

So visiting http://www.mywebsite.com/go/somewhere-else will redirect to http://www.my-affiliate-link.com/.

On my site, I use "nofollow" to tell the search engines not to follow the link. The 307 status code means "temporary redirect".

<a href="http://www.mywebsite.com/go/somewhere-else" rel="nofollow">Click here!</a>


Need Your Help

Cypher Query to get duplicate(same id) relationship between nodes

neo4j cypher

I have city nodes connected to each other by HAS_BUS relationships.

WSO2: API Manager; calling publish api from webservice

wso2 wso2carbon

Is it possible to call the WSO2 API Manager:: publisher api, as described in: