【问题标题】:.htaccess root domain require www and sub domain not require www.htaccess 根域需要 www,子域不需要 www
【发布时间】:2011-10-19 10:19:50
【问题描述】:

我想像下面这样设置域:

example.com -> www.example.com

example.com/page -> www.example.com/page

www.example.com -> 不重定向

www.example.com/page -> 不重定向


subdomain.example.com -> 不重定向

subdomain.example.com/page -> 不重定向

www.subdomain.example.com -> subdomain.example.com

www.subdomain.example.com/page -> subdomain.example.com/page


http 和 https 也应该可以工作。

我从.htaccess to require WWW for domain, but allow subdomain if existing with no hard coding得到代码

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ 
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

它对所有人都适用,但以下情况除外:

www.subdomain.example.com -> www.subdomain.example.com

www.subdomain.example.com/page -> www.subdomain.example.com/page

子域应该包含“www.”前缀,但它仍然带有“www”。前缀。

感谢教程。

【问题讨论】:

    标签: .htaccess redirect subdomain web


    【解决方案1】:

    在你拥有的规则之后添加这个

    RewriteCond %{HTTPS} ^on$
    RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$
    RewriteRule ^ https://%1%{REQUEST_URI} [L]
    
    RewriteCond %{HTTPS} !^on$
    RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$
    RewriteRule ^ http://%1%{REQUEST_URI} [L]
    

    我还没有测试过,但我很确定我的理论是可靠的。至少假设您示例中的 HTTPS 部分有效。

    【讨论】:

    • 现在我将这段代码粘贴到我的 .htaccess 文件中:RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$RewriteCond %{HTTPS}s ^on(s)|RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$RewriteCond %{HTTPS}s ^on(s)|RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]对于子域部分仍然没有什么问题: www.subdomain.domain.com -> subdomain.domain.com(完成),www.subdomain.domain.com/page -> page/(问题)
    • 改变了我的答案。需要两个重写规则,因为我错过了 htaccess 文档中的一些内容。
    猜你喜欢
    • 2013-11-07
    • 2017-07-31
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2016-01-21
    • 2014-06-02
    • 1970-01-01
    相关资源
    最近更新 更多