【问题标题】:Redirect Url sub-domain to sub-directory将 Url 子域重定向到子目录
【发布时间】:2019-07-16 11:43:00
【问题描述】:

我正在尝试重定向网址。当有人键入xyz.example.com 时,它会重定向到example.com/zyz。 我没有子域。 我正在尝试当有人输入确切的字符串xyz.example.com 然后重定向到example.com/xyz。 我正在开发多网站。

xyz.example.com

这不是现有的域。

example.com/xyz 

这是多网络站点 url,不存在子域

【问题讨论】:

  • 如果您不拥有子域,则无法重定向子域。
  • 所以您没有子域xyz.example.com 但想要重定向到您的页面?这是不可能的。
  • LLJ97@it's any thing like that。如果有人输入 xyz.example.com 。然后从 url 中获取 url 字符串,然后匹配 url 中的第一个字符串,然后重定向到其他 url。

标签: php .htaccess redirect


【解决方案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

【讨论】:

  • Vidal@ 在explode this $arr = explode('.',$hostname); 之后,我们可以使用strop 作为条件吗,如果$hostname 中的xyz 字符串则只重定向
  • 该示例将根据您的要求将所有 subdmoains 重定向到它们的文件夹。前任。 someSub.domain.com -> domain.com/someSub ,如果你只想重定向一个特定的子域,你可以做更多 if/else of case。
  • Vidal @但我必须匹配字符串。如果你有任何想法那么你能更新这个答案吗?我知道我们走在正确的道路上
猜你喜欢
  • 1970-01-01
  • 2011-06-28
  • 2013-07-17
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多