【问题标题】:.htaccess in root not to interfere with subdomain rules?root 中的 .htaccess 不会干扰子域规则?
【发布时间】:2013-09-12 14:34:02
【问题描述】:

在主域的根目录中,我有这个 .htaccess 规则,这样每个非 www url 都会重定向到 www.domain.com-urls:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

然后我在我的 VPS 上通过 Cpanel 设置了一个子域(它将子域添加为子目录,即 /sub),即:

sub.domain.com

现在所有的 url 都被重定向了 到:

www.subdomain.com urls

我只需要非 www 网址,即:

www.sub.domain.com -> sub.domain.com
sub.domain.com -> sub.domain.com

如何使 domain.com 的根 .htaccess 不会干扰子域 .htacess 规则?

因为我要添加更多的子域,这将需要他们自己的 htaccess 规则,并且都是非 www,我该怎么办?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite subdomain


    【解决方案1】:

    您可以添加附加条件并仅检查“domain.com”:

    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    

    或:

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^[^.]+\.[a-z]{2,4}$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 2012-06-11
      • 2010-09-26
      • 2011-11-21
      • 2012-07-31
      • 2016-01-19
      相关资源
      最近更新 更多