根据.htaccess中的域/子域更改DirectoryIndex

Tec*_*lco 11 apache .htaccess mod-rewrite redirect directoryindex

我有一个域和一个子域(用于移动和客户端)的共享主机.每个域和子域都有不同的默认索引页.托管公司告诉我将所有内容放在我的.htaccess文件中,因为我无法访问httpd.conf.

我想要做的是:

  1. 如果用户访问domain1.com,则DirectoryIndex应该是:index.html
  2. 如果用户访问mobile.domain1.com,则DirectoryIndex应该是:mobile-index.html
  3. 如果用户访问post.domain1.com,则DirectoryIndex应该是:post.php
  4. 如果用户访问vote.domain1.com,则DirectoryIndex应该是:vote.php

编辑:另外,如果我去domain1.com/page/,DirectoryIndex应该是:index.html.如果我去mobile.domain1.com/page/,DirectoryIndex应该是:mobile-index.html

我可以在.htaccess文件中添加什么来更改DirectoryIndex每个子域?

非常感谢你

Boo*_*eus 18

<IfDefine>不是这样的.<IfDefine>仅在apache启动时运行.你应该使用mod_rewrite解决方案.查看@tzakrajs的答案.

您可以在.htaccess文件中使用它:

SetEnvIf Host ^www\. page=www
SetEnvIf Host ^mobile\. page=mobile
rewriterule ^.*$ test.php?subdomain=%{ENV:page} [QSA,L]
Run Code Online (Sandbox Code Playgroud)

只需使用配置所有子域SetEnvIf,然后让PHP发挥其魔力.

  • 谢谢你的澄清.感谢SetEnvIf,我认为这更容易实现(而不是创建4个不同的文件).谢谢 (2认同)