【问题标题】:PHP redirect based on HTTP_HOST/SERVER_NAME within same domain基于同一域内的 HTTP_HOST/SERVER_NAME 的 PHP 重定向
【发布时间】:2011-04-22 06:34:05
【问题描述】:

我正在尝试使用 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();

                }
?>

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

【问题讨论】:

  • 您打开的初始网址是什么?
  • 仅供参考,在您的第一个示例中,位置必须是完全限定的 URL,而不是相对路径。
  • @Nirmal 无论是相对 URL 还是绝对 URL,都存在同样的问题

标签: php redirect dns


【解决方案1】:

好的,我就是这样解决的:

<?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 字段,否则它将不起作用。

【讨论】:

    【解决方案2】:

    因为您正在重定向到同一个服务器(example.dk)并且您的代码在循环中一次又一次地执行。

    改用此代码:

    $domain = $_SERVER["SERVER_NAME"];
    if (($domain == "example.dk" ||
       $domain == "www.example.dk") && !$_GET['redirected'])  { 
       header("location: /index.php/da/forside?redirected=1"); 
    }
    

    【讨论】:

    • PHP 文档说: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. 所以最好在您的示例中使用绝对 URI。
    • 它有效,但是当我点击该页面上的任何内容时,我无法上网。我只是卡在那个页面上。另外,这对搜索引擎友好吗?
    • @nctrnl 你的其他网页网址是什么?
    • @rahim asgari 喜欢这样:example.dk/index.php/da/axeexample.dk/index.php/da/sword
    • 它们也在 example.dk 域中。所以他们也会重定向到 /index.php/da/forside。
    【解决方案3】:

    最常见的重定向错误是重定向循环。

    1. 在您的第一个示例之后脚本真的结束了吗?
    2. $host 从何而来?

    另外,SERVER_NAME 通常是 apache 配置的全局名称,HTTP_HOST 确实是这样做的正确方法。

    HTTP_HOST 可能包含端口号,请记住这一点。

    那么你的脚本的 url 是什么,你要重定向到哪里?

    一种简单的调试方法是回显 HTTP_HOST 的内容,而不是调用 header(),也调用'echo'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-17
      • 2023-04-04
      • 2020-05-15
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2011-06-22
      • 2018-02-23
      相关资源
      最近更新 更多