开发者

Redirect site alias with php?

开发者 https://www.devze.com 2023-04-05 18:25 出处:网络
Say I have two domain names:www.somesite.c开发者_JS百科om and www.anothersite.com and both go to www.somesite.com (anothersite.com is an alias).

Say I have two domain names: www.somesite.c开发者_JS百科om and www.anothersite.com and both go to www.somesite.com (anothersite.com is an alias).

Can I, with the index.php on somesite.com, redirect a visitor if they typed in www.anothersite.com (with PHP)?


Yes, you can check against $_SERVER['HTTP_HOST']. If it is anothersite.com, redirect using header(). Alternatively, you could use .htaccess with 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.


<?php
  header('Location: http://www.somesite.com/');
?>

-edit- This only redirects, did not read the question properly.


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


<?
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.

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

Thank you for your answers. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消