【问题标题】:Change the DirectoryIndex based on a domain/sub-domain in .htaccess根据 .htaccess 中的域/子域更改 DirectoryIndex
【发布时间】:2011-11-08 08:10:01
【问题描述】:

我有一个共享主机,带有一个域和一个子域(用于移动设备和客户端)。每个域和子域都有不同的默认索引页面。托管公司告诉我将所有内容都放在我的 .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

为了更改每个子域的DirectoryIndex,我可以在我的 .htaccess 文件中添加什么内容?

非常感谢你

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect directoryindex


    【解决方案1】:

    您可以像这样使用 oyur .htaccess 文件进行设置:

    RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
    RewriteRule DirectoryIndex index.html
    
    RewriteCond %{HTTP_HOST} ^mobile.domain.com$ [NC]
    RewriteRule DirectoryIndex mobile-index.html
    
    ...
    

    【讨论】:

    • @laxman 你不需要因为它不适合你而投反对票
    【解决方案2】:

    <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 发挥它的魔力。

    【讨论】:

    • 感谢您的澄清。感谢 SetEnvIf,我认为这更容易实现(而不是创建 4 个不同的文件)。谢谢
    • 换句话说:<IfDefine> 在 Apache 启动后不是动态的。
    【解决方案3】:

    试试这个:

    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^domain1.com$
    RewriteRule ^.*/$ index.html [R=302,L]
    RewriteCond %{HTTP_HOST} ^mobile.domain1.com$
    RewriteRule ^.*/$ mobile-index.html [R=302,L]
    RewriteCond %{HTTP_HOST} ^post.domain1.com$
    RewriteRule ^.*/$ post.php [R=302,L]
    RewriteCond %{HTTP_HOST} ^vote.domain1.com$
    RewriteRule ^.*/$ vote.php [R=302,L]
    

    【讨论】:

    • 感谢它适用于登录页面,但不适用于子文件夹。例如 mobile.domain1.com/page/ 应该加载 mobile-index.html
    • 好的,我已经修改了规则,以便它们可以匹配任何子目录(例如:domain1.com/fdskjfkls/ 将重定向到 index.html)
    • 它会但不会重定向到:domain1.com/fdskjfkls/index.html 和 mobile.domain1.com/fdskjfkls/mobile.html
    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2015-11-16
    • 1970-01-01
    • 2017-01-23
    相关资源
    最近更新 更多