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

标签 apache .htaccess mod-rewrite redirect directoryindex

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

我想做的是这样的:

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

  • 编辑:
    另外,如果我去 domain1.com/page/DirectoryIndex应该是:index.html .如果我去 mobile.domain1.com/page/DirectoryIndex应该是:mobile-index.html
    我可以在我的 .htaccess 文件中放入什么来更改 DirectoryIndex每个子域?

    非常感谢你

    最佳答案

    <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]
    

    只需使用 SetEnvIf 配置您的所有子域然后让 PHP 发挥它的魔力。

    关于apache - 根据 .htaccess 中的域/子域更改 DirectoryIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7269685/

    相关文章:

    apache - 如果 url 匹配,.htaccess 跳过所有规则

    linux - 为什么我的 IP 地址总是以 hosts.deny 文件结尾?

    正则表达式重写 AND 模式

    .htaccess - 删除 index.php,强制 ssl,并将 www 重定向到 .htaccess 中的非 www

    php - .htaccess 带有 MVC 应用程序 Controller 名称的重定向 url

    apache - 当tomcat中的 war 停止时如何自动重定向到另一个页面

    python - flask 错误 : 414 Request-URI Too Large

    .htaccess - htaccess 重写排除不起作用

    apache - .htaccess 将敏感区域的 http 重定向到 https

    authentication - .htaccess mod-rewrite 与子文件夹身份验证冲突