.htaccess将不带www的域名跳转到带www的域名不生效
先来看解决方法:
在如下的虚拟机配置中
<VirtualHost *:80>
DocumentRoot /alidata/www/test
ServerName www.test.com
ServerAlias test.com abc.com
ServerAlias *.test.com
<Directory "/alidata/www/test">
Options None
AllowOverride all
Order allow,deny
Allow from all
</Directory>
ErrorLog "/alidata/log/httpd/test-error.log"
CustomLog "/alidata/log/httpd/test.log" common
</VirtualHost>
在上面的虚拟机配置项中:
ServerAlias 可以设置多个别名(域名),可以有多个 ServerAlias 还可以设置泛域名 如:*.test.com,多级泛域名也可以 如:*.one.test.com
当一个域名时,服务器会默认将不带www的域名解析到网站的目录下,当多个域名时只有一个不带www的域名会被默认解析的。还有一个就没有解析,也没法解析必须指定,要不然谁知道要干嘛。这时候设置.htaccess也不会有用的,因为域名根本就没有指定到改目录,而.htaccess文件只适用所在的目录。
.htaccess的设置如下:
<IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{HTTP_HOST} ^test.com [NC] RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </IfModule>
如果不想跳转而是 test.com 和 www.test.com 区别独立的使用,就不用设置.htaccess中的跳转部分了。当然这样不建议,因为如果两个域名的内容是一样的,百度收录会把它们看成两个网站。