简体   繁体   English

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

[英]PHP redirect based on HTTP_HOST/SERVER_NAME within same domain

I am trying to redirect to a specific path based on HTTP_HOST or SERVER_NAME with a PHP-script. 我正在尝试使用PHP脚本重定向到基于HTTP_HOST或SERVER_NAME的特定路径。

I have tried these scripts: 我试过这些脚本:

1. 1。

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

2. 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();

                }
?>

And other similar scripts. 和其他类似的脚本。 Either the page loads forever or the browser returns some redirection error. 页面永远加载或浏览器返回一些重定向错误。

Ok, this is how I solved it: 好的,这就是我解决它的方式:

<?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"); 
}

?>

It appears I need the REQUEST_URI field, otherwise it wouldn't work. 看来我需要REQUEST_URI字段,否则它将无法工作。

The most common redirection error is a redirection loop. 最常见的重定向错误是重定向循环。

  1. Does the script really end after your first example? 在你的第一个例子后,脚本真的结束了吗?
  2. Where does $host come from? $ host来自哪里?

Also, SERVER_NAME is usually an apache configured global name, HTTP_HOST is really the right way to do this. 此外,SERVER_NAME通常是apache配置的全局名称,HTTP_HOST实际上是正确的方法。

HTTP_HOST may contain the port number, keep this in mind. HTTP_HOST可能包含端口号,请记住这一点。

So what's the url of your script, and where are you redirecting to? 那么你的脚本的url是什么,你在哪里重定向到?

A simple way to debug is to echo the contents of HTTP_HOST and instead of calling header(), also call 'echo'. 一种简单的调试方法是回显HTTP_HOST的内容,而不是调用header(),也称为'echo'。

Because your are redirecting to the same server (example.dk) and your code executes again and again in a loop. 因为您正在重定向到同一服务器(example.dk)并且您的代码在循环中一次又一次地执行。

use this code instead: 请改用此代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP中的HTTP_HOST和SERVER_NAME有什么区别? - What is the difference between HTTP_HOST and SERVER_NAME in PHP? $ _SERVER [&#39;HTTP_HOST&#39;]和$ _SERVER [&#39;SERVER_NAME&#39;]检测问题 - $_SERVER['HTTP_HOST'] AND $_SERVER['SERVER_NAME'] Detection Issue PHP $ _SERVER ['HTTP_HOST']与$ _SERVER ['SERVER_NAME'],我是否正确理解了手册页? - PHP $_SERVER['HTTP_HOST'] vs. $_SERVER['SERVER_NAME'], am I understanding the man pages correctly? $ _SERVER [&#39;SERVER_NAME&#39;]和$ _SERVER [&#39;HTTP_HOST&#39;]都与实际网址不同 - $_SERVER['SERVER_NAME'] and $_SERVER['HTTP_HOST'] both different than actual URL php $ _SERVER [&#39;HTTP_HOST&#39;]始终是同一个域,无论我从哪个域请求 - php $_SERVER['HTTP_HOST'] is always same domain no matter from which domain i request 安全的php主机名信息 - $ _SERVER [&#39;HTTP_HOST&#39;]替代方案 - secure php host name information - $_SERVER['HTTP_HOST'] alternative 在Chrome中,基于$ _SERVER [&#39;HTTP_HOST&#39;]的PHP加载CSS失败 - PHP load CSS based on $_SERVER['HTTP_HOST'] fails in Chrome $ _SERVER [HTTP_HOST]的域错误? - Wrong domain with $_SERVER[HTTP_HOST]? $ _SERVER [HTTP_HOST]是重定向问题的原因吗? - Is $_SERVER[HTTP_HOST] the cause of redirect issues? 使用HTTP_HOST获取没有端口的域名 - Get domain name without port using HTTP_HOST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM