在HostMonster的虚拟主机中,默认是将public_html(www目录是public_html目录的链接文件)目录作为主域名的根目录的;而子域名或附加域名的根目录是在public_html目录的同名目录中。这样就造成了主域名的程序文件和子域名根目录混淆在一起,看上去很乱。幸好我们有.htaccess文件(需是apache服务器),利用它的重写功能将主域名的根目录”重定向”到一个子目录中,即主域名的根目录与其它域名的根目录都处于public_html目录中,这样看上去就干净多了。

具体的写法如下

# .htaccess main domain to subfolder redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.
 
# 打开apache重写引擎
RewriteEngine on
 
# 设置重写条件1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
 
# 设置重写条件2
RewriteCond %{REQUEST_URI} !^/subfolder/
 
# 设置重定条件3(非文件或目录请求)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 
# 满足上述三个条件则执行重写规则1
RewriteRule ^(.*)$ /subfolder/$1
 
# Change yourdomain.com to be your main domain again.
# Change ‘subfolder’ to be the folder you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
# 重写规则2
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

说明:将yourmaindomain.com替换成你的主域名;subfolder是你更改后的主域名的根目录名;最后将该.htaccess文件放到public_html目录即可。现在开始清理你的public_html目录吧,还你一个干干净净的主目录。

英文版

# HostMonster.com
# .htaccess main domain to subfolder redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.
 
# Do not change this line.
 
RewriteEngine on
 
# Change yourdomain.com to be your main domain.
 
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
 
# Change ’subfolder’ to be the folder you will use for your main domain.
 
RewriteCond %{REQUEST_URI} !^/subfolder/
 
# Don’t change this line.
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 
# Change ’subfolder’ to be the folder you will use for your main domain.
 
RewriteRule ^(.*)$ /subfolder/$1
 
# Change yourdomain.com to be your main domain again.
# Change ’subfolder’ to be the folder you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
 
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

subfolder替换你在public_html/下建立的目录,yourmaindomain.com替换为你的域名。

1 对 “绑定HostMonster主域名到任意目录”的想法;

发表评论

电子邮件地址不会被公开。 必填项已用*标注