一个使用HostMonster的用户想将主域名指向其中一个文件夹/目录,问我会不会弄,我随即就说通过.htaccess重写就可以了,虽然对.htaccess有一定的了解,可是真正要实现这个功能的重写,还不知道具体怎么实现,尝试了几次都出现这样或那样的问题。无奈中~~只好上网寻找答案~~
找这种答案还是很容易的,很快就在HostMonster的知识库里找到了,那里有一篇文章专门写如何用.htaccess重写将主域名指向一个子目录/文件夹。
在虚拟主机中,主域名是使用public_html目录/文件夹作为主域名的缺省目录,主域名网站的文件和程序都是放在public_html目录下,附加的域名(addon domains)使用public_html目录/文件夹下的子目录/子文件夹。有的人可能觉得public_html目录/文件夹下的会看起来比较乱,因此想把主域名也指向其中一个子目录/文件夹。这样就需要利用.htaccess的重写功能。
具体的写法如下:
# .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]






bolo says:
May 26th, 2009 at 7:10 pm
有了个linux主机,要好好学学这一个了
Feng says:
July 9th, 2009 at 5:41 pm
这个方法不错,在Godaddy的linux主机上同样可行
谢谢站长
Beishan says:
July 9th, 2009 at 6:01 pm
呵呵,能用htaccess的都可以
infoseek says:
July 11th, 2010 at 1:17 am
谢谢beishan的文章,谢谢悠总帮我找到了这里,呵呵
山头人 says:
September 25th, 2010 at 9:45 am
这个我转走。
最弱 says:
April 11th, 2011 at 10:48 pm
不错。试用!
HostMonster主机上利用.htaccess自定义错误页 says:
December 12th, 2011 at 2:07 pm
[…] 在很多美国主机上基本都可以自定义错误页,这里以HostMonster主机为例,利用.htaccess文件自定义错误页,基本上在所有的美国Linux主机上都可以利用.htaccess文件自定义错误页面(Custom Error Pages ) [用.htaccess重写将主域名指向一个子目录/文件夹] […]
七夜 says:
November 29th, 2012 at 12:03 pm
如果访问子目录 结尾没加 / 会造成网址跳转错误
当访问www.xxx.com/xxx 的时候(后面不带/的时候) 会跳转成 http://www.xxx.com/你的子目录/xxx/ 导致无法访问或影响SEO效果和用户体验
潇遥 says:
May 23rd, 2013 at 8:32 pm
这样跳转符合seo吗?我原来的方法我忘了,原来要修改index.php,可以设置跳转提示语和跳转时间。原来输入主域名跳转到主域名/目录名,现在你这种方法输入主域名就直接访问了安装在目录下的网站。不知道这样对搜索引擎友好不友好?我的博客放在blog这个目录下,首页还没建好,因此就跳转一下的。