我正在尝试使用 PHP 脚本重定向到基于 HTTP_HOST 或 SERVER_NAME 的特定路径。
我试过这些脚本:
1.
$domain = $_SERVER["SERVER_NAME"];
if (($domain == "example.dk") ||
($domain == "www.example.dk")) {
header("location: /index.php/da/forside");
}
?>
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();
}
?>
和其他类似的脚本。要么页面永远加载,要么浏览器返回一些重定向错误。
最佳答案
好的,我就是这样解决的:
<?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");
}
?>
看来我需要 REQUEST_URI 字段,否则它不会工作。
关于基于同一域内的 HTTP_HOST/SERVER_NAME 的 PHP 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3880321/