【问题标题】:How to call different index files for main domain and sub domains?如何为主域和子域调用不同的索引文件?
【发布时间】:2012-09-01 00:43:48
【问题描述】:

我有一个名为 www.mysite.com 的网站。当 url 是 www.mysite.com 时,我想调用 index1.php 文件(这是我的主域)。

如果 url 来自 test1.mysite.com、test2.mysite.com 等子域,那么我想调用 index2.php 文件。

在htaccess中怎么做?

【问题讨论】:

    标签: .htaccess url subdomain


    【解决方案1】:

    如果它们都在同一个文档根目录中,请在文档根目录中的 htaccess 文件中添加如下内容:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
    RewriteRule ^$ /index1.php [L]
    
    RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.com$ [NC]
    RewriteRule ^$ /index2.php [L]
    

    如果您有指向需要重写的目录索引的链接,您可以根据具体情况进行处理,例如:

    RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
    RewriteRule ^(.*)/index\.(php|html|htm)$ /$1/index1.php [L]
    
    RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.com$ [NC]
    RewriteRule ^(.*)/index\.(php|html|htm)$ /$1/index2.php [L]
    

    【讨论】:

    • 如果我想从根目录调用index1.php,从其他子文件夹调用index2.php,请问如何修改这段代码?
    • @jeeva 这与主域和子域有什么关系?
    • RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC] RewriteRule ^$ /index1.php [L] RewriteCond %{HTTP_HOST} !^(www\.) ?mysite\.com$ [NC] RewriteRule ^$ /index2.php [L] 我已将此代码添加到我的 .htaccess 文件中。现在主域不工作(www.mysite.com),但子域(test1.mysite.com,test2.mysite.com)工作正常。现在两个索引文件都在根目录中。
    • 感谢您的回复.. 是的,当我在 url 中提供 mysite.com/index1.php 时,它工作正常。但我不希望 index1.php 或 index2.php 显示在 url 中。等待您的回复。
    • @jeeva 不知道出了什么问题,我将顶部的 2 条规则放入一个空白的 htaccess 文件中,然后转到 http://mysite.com/http://www.mysite.com/ 并看到我的 index1.php 的内容文件。
    猜你喜欢
    • 1970-01-01
    • 2021-05-14
    • 2017-01-31
    • 2016-12-19
    • 2015-06-23
    • 2021-12-11
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    相关资源
    最近更新 更多