PHP重定向基于同一域内的HTTP_HOST/SERVER_NAME

jma*_*man 5 php dns redirect

我正在尝试使用PHP脚本重定向到基于HTTP_HOST或SERVER_NAME的特定路径.

我试过这些脚本:

1.

$domain = $_SERVER["SERVER_NAME"];
if (($domain == "example.dk") ||
   ($domain == "www.example.dk")) { 
   header("location: /index.php/da/forside"); 
}
?>
Run Code Online (Sandbox Code Playgroud)

2.

switch ($host) {

        case 'example.dk':
                header("HTTP/1.1 301 Moved Permanently");
                header("Location: http://www.example.dk/index.php/da/forside/");
                exit();

        case 'www.example.dk':
                header("HTTP/1.1 301 Moved Permanently");
                header("Location: http://www.example.dk/index.php/da/forside/");
                exit();



        default:
                header("Location: http://www.example.se");
                exit();

                }
?>
Run Code Online (Sandbox Code Playgroud)

和其他类似的脚本.页面永远加载或浏览器返回一些重定向错误.

jma*_*man 8

好的,这就是我解决它的方式:

<?php
$domain = $_SERVER["SERVER_NAME"];
$requri = $_SERVER['REQUEST_URI'];
if (($domain == "www.example.dk" && $requri == "/index.php"  ||
   $domain == "example.dk") )  { 
   Header( "HTTP/1.1 301 Moved Permanently" ); 
   header("location: http://www.example.dk/index.php/da/forside"); 
}

else if (($domain == "uk.example.dk" && $requri == "/index.php"  ||
   $domain == "www.uk.example.dk") )  {
   Header( "HTTP/1.1 301 Moved Permanently" );    
   header("location: http://uk.example.dk/index.php/en/uk/home"); 
}

else if (($domain == "www.example.se" && $requri == "/index.php"  ||
   $domain == "example.se") )  { 
   Header( "HTTP/1.1 301 Moved Permanently" ); 
   header("location: http://example.se/index.php/sv/hem"); 
}

?>
Run Code Online (Sandbox Code Playgroud)

看来我需要REQUEST_URI字段,否则它将无法工作.