Linux区


.htaccess实现一个空间做多个独立网站需要注意的事项

注意:Linux空间用.htaccess最好,不建议用windows主机。以下,分网站名为subsite.com,主网站名为mainsite.com

1,最先要做的是将subsite.com解析到跟mainsite.com一样的ip或cname。

2,经过试验发现:它不适用于根目录放wordpress的空间,或者不适合华夏名网的空间,两个结论二选一,没具体测试过,反正试验失败之后,我又用到了其他空间商的linux空间(主网站为dedecms的)。

3,根目录的.htaccess的写法:

<IfModule mod_rewrite.c>
RewriteEngine On

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

#以下2行是必须的,否则会出现重定向错误!index.html可以换成其他的格式如index.php等
RewriteCond %{HTTP_HOST} ^www.mainsite.com$
RewriteRule ^(/)?$ /index.html [L]
#以上2行是必须的,否则会出现重定向错误!index.html可以换成其他的格式如index.php等

RewriteCond %{HTTP_HOST} ^(www\.)?subsite\.com$ [NC]
#以下一行的意思是访问subsite.com的时候跳转到subfolder。最后的$1 [L]的意思是所有文件都用subfolder下的文件。千万不要掉了$1。这样就避免了subsite使用mainsite的同名文件的尴尬。
RewriteRule ^(.*)$ subfolder/$1 [L]

ErrorDocument 404 /404.html
</IfModule>

4,subfolder目录的.htaccess的写法:

<IfModule mod_rewrite.c>
RewriteEngine On
# 禁止通过非本网站域名访问,给人的感觉就好像这个subfolder独立网站跟mainsite主网站没有任何关系一样,还能自动跳转,挺好!
RewriteCond %{HTTP_HOST} !^(www.)?subsite.com$ [NC]
RewriteRule ^.*$ [L,R=404]
ErrorDocument 404 /404.html
</IfModule>

5,因为subsite的mainsite不能有任何同名文件所以要将经常会有重复的文件名如favicon.ico或index.htm修改为其他文件名,以防止载入mainsite的同名文件,如将favicon.ico修改成favicon_subsite.ico,并在每个网页里都设置favicon为favicon_subsite.ico。

6,分网站的所有页面的主页链接最好写成/,不要写成index_subsite.htm或default_subsite.htm,以免给访客造成一种主页非主页的感觉。

2016.05.11说明:以上第5、6条已经没必要了。修改优化后的.htaccess文件已经可以避免subsite使用mainsite同名文件的尴尬】

 

参考1:http://www.freehao123.com/htaccess-godaddy/

参考2:http://www.timle.cn/htaccess-more-website.html

相关博文



发表评论

电子邮件地址不会被公开。