将网址子域重定向到子目录

时间:2019-07-16 11:43:00

标签: php .htaccess redirect

我正在尝试重定向url。当有人键入xyz.example.com时,其将重定向到example.com/zyz。 我没有子域。 我在尝试输入准确的字符串xyz.example.com然后重定向到example.com/xyz时遇到问题。 我在多网站上工作。

xyz.example.com

这不是现有的Domain。

example.com/xyz 

这是一个多网络站点url,没有为此提供的子域

1 个答案:

答案 0 :(得分:1)

您将必须配置apache虚拟主机。

<VirtualHost *:80>
    DocumentRoot "/www/subdomainFolder"
    ServerName subdomain.example.com

    # Other directives here
</VirtualHost>

另一方面,如果您有一个通配符* IN A 192.0.2.1,则可以将所有子域映射到您的主域ip。您可以:

<?php
   $hotsname = $_SERVER["HTTP_HOST"];
   $arr = explode('.',$hostname);
   $url='http://example.com/';
   if(count($arr)>2){
      // we have a subdomain

      // redirect xyz only.
      if($arr[0] == "xyz"){
          header("location:" $url. $arr[0]);
          exit();
      }
   }

   // Continue normal operation, no redirect.

?>

Apache VirtualHost Documentation

相关问题