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

标签 php redirect dns

我正在尝试使用 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/

相关文章:

php - Symfony 安全 : Is there an event when a user is authenticated from the session?

redirect - Symfony 在用户条件下登录后重定向

Javascript/JQuery - 如果达到一定大小,则在窗口调整大小时重定向

wordpress - 使用 .htaccess - wordpress 将一个类别重定向到另一个类别

javascript - 使用 javascript 预取 DNS

ruby-on-rails - 我的 Heroku 和 Godaddy SSL 配置有什么问题?

javascript - 用 * 搜索重新定义字符串的一部分

php - Wordpress:更新用户角色时,功能不会改变

Docker:从 docker 访问 VPN 域

javascript - 从数据库中调用数据进行计算(PHP)