【问题标题】:How to point all subdomains to index.php [closed]如何将所有子域指向 index.php [关闭]
【发布时间】:2013-04-22 00:25:11
【问题描述】:

我正在尝试托管一个网站,该网站上的用户个人资料页面带有独特的子域,例如 username.sitename.com。我想要一个 Apache/VirtualHost 解决方案,用于将 *.sitename.com 指向 sitename.com/index.php,我可以在其中进行分类,哪个子域带有个人资料页面并且会显示 404 错误。

请帮我设置 .htaccess

【问题讨论】:

  • 使用 .htaccess 文件无法做到这一点。
  • Catchall DNS 记录还是现在?

标签: apache .htaccess vhosts


【解决方案1】:

您只需要正确的 VirtualHost 设置。

<VirtualHost *:80>
  ServerName example.com 
  ServerAlias *.example.com

  [...] 

</VirtualHost>

由于 index.php 在 DirectoryIndex 中,它会被自动调用,无论调用哪个域。做动态反应你可以通过$_SERVER['HTTP_HOST'] 变量访问请求的主机。

【讨论】:

    【解决方案2】:

    最好通过 httpd.conf 文件(或包含的配置文件)中的服务器级别的 301 重定向来执行此操作。您可以在 VirtualHost 本身中设置重定向,而不是创建 VirtualHost 并使用 .htaccess 文件对每个域进行服务器应答和过滤。

    您可以通过mod_rewrite 做到这一点

    <VirtualHost *>
      ServerName subdomain.example.com
      RewriteEngine on
      RewriteRule ^/(.*)$ http://example.com/index.php [L,R=301]
    </VirtualHost>
    
    <VirtualHost *>
      ServerName subdomain2.example.com
      RewriteEngine on
      RewriteRule ^/(.*)$ http://example.com/404.php [L,R=301]
    </VirtualHost>
    

    或者如果你正在运行mod_alias,你可以使用这个:

    <VirtualHost *>
      ServerName subdomain.example.com
      Redirect 301 / http://example.com/index.php
    </VirtualHost>
    
    <VirtualHost *>
      ServerName subdomain2.example.com
      Redirect 301 / http://example.com/404.php
    </VirtualHost>
    

    【讨论】:

      猜你喜欢
      • 2015-03-21
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多