简体   繁体   English

用php重定向站点别名?

[英]Redirect site alias with php?

Say I have two domain names: www.somesite.com and www.anothersite.com and both go to www.somesite.com (anothersite.com is an alias). 假设我有两个域名:www.somesite.com和www.anothersite.com,都去了www.somesite.com(anothersite.com是别名)。

Can I, with the index.php on somesite.com, redirect a visitor if they typed in www.anothersite.com (with PHP)? 如果访问者输入www.anothersite.com(使用PHP),我是否可以使用somesite.com上的index.php重定向访问者?

Yes, you can check against $_SERVER['HTTP_HOST']. 是的,您可以检查$ _SERVER ['HTTP_HOST']。 If it is anothersite.com, redirect using header(). 如果它是anothersite.com,请使用header()进行重定向。 Alternatively, you could use .htaccess with mod_rewrite. 或者,您可以将.htaccess与mod_rewrite结合使用。

Depends. 要看。 If both domains just start the same script, you can check which domain was used. 如果两个域都只启动相同的脚本,则可以检查使用了哪个域。 If you redirect (301 or other) from anothersite.com to somesite.com, it becomes a new request, and you cannot see that the user actually typed anothersite.com. 如果您从othersite.com重定向(301或其他)到somesite.com,它将成为一个新请求,并且您看不到用户实际键入了anothersite.com。

if (false !== strpos($_SERVER['HTTP_HOST'], "anothersite.com")){
    header("Location: http://somesite.com");
    die();
}
<?php
  header('Location: http://www.somesite.com/');
?>

-edit- This only redirects, did not read the question properly. -edit-这仅重定向,未正确阅读问题。

<?
if(strpos($_SERVER["SERVER_NAME"], 'anothersite.com') !== false) {
  header ("HTTP/1.1 301 Permanent Redirect "); // you don't need that
  header ('Location: http://somewhere.else.com');
  exit();
}


?>

Found the answer. 找到了答案。

I needed to use HTTP_X_HOST, not HTTP_HOST. 我需要使用HTTP_X_HOST,而不是HTTP_HOST。

<?PHP
if($_SERVER['HTTP_X_HOST']=='anothersite.com'){
    header('Location: http://www.somesite.com/anothersite/');
}
?>

Thank you for your answers. 谢谢您的回答。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM