mod从.domain重写为www.domain htaccess

时间:2014-01-13 17:02:36

标签: apache .htaccess mod-rewrite

如何将htaccess文件扩展为.domain.com - > www.domain.com?

  <IfModule mod_rewrite.c>
        RewriteEngine On
        #catch potential subpages first
        RewriteRule ^([a-z-]+)/([0-9a-z-]+)/?$ index.php?page=$1&subpage=$2 [L,NC,QSA]
        #catch parent page without subpage
        RewriteRule ^([a-z-]+)/?$ index.php?page=$1 [L,NC,QSA]
  </IfModule>

是否可以不定义domain.com?并拥有任何域的通用规则?

通常我会这样做:

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

但是我如何将它与上面的指令联合起来?

1 个答案:

答案 0 :(得分:1)

你的规则如下:

RewriteEngine On

# anything.com to www.anything.com
RewriteCond %{HTTP_HOST} ^[^.]+(\.[^.]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]

#catch potential subpages first
RewriteRule ^([a-z-]+)/([0-9a-z-]+)/?$ index.php?page=$1&subpage=$2 [L,NC,QSA]

#catch parent page without subpage
RewriteRule ^([a-z-]+)/?$ index.php?page=$1 [L,NC,QSA]

PS:如果您想为子域添加www,请使用此规则:

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