1

内部ページにリンクしているときにユーザーを外部サイトにリダイレクトするにはどうすればよいですか?

次のような例を見てきました。

  • example.com/go/ksdjfksjdhfls
  • example.com/?go=http://www.new-example.com
  • ... などなど...

これはphpでどのように達成されますか?

これには SEO に関して賛否両論がありますか?

4

5 に答える 5

5

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?

于 2012-06-10T06:16:20.763 に答える
1

put this is beginning before any output to browser

<?
header('location:example.com\index.php');
?>
于 2012-06-10T06:15:56.577 に答える
1

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');
?>
于 2012-06-10T06:18:40.937 に答える
0

これには .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>

于 2012-06-22T00:17:21.543 に答える
0

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>
于 2012-06-10T06:10:19.653 に答える