用.htaccess重写将主域名从网站根目录重新指向一个子目录
一个使用HostMonster(实际上应该是所有支持重写.htaccess文件的空间都可以通过次方法实现)的用户想将主域名指向其中一个文件夹/目录,问我会不会弄,我随即就说通过.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]
Related posts:
- 用.htaccess实现域名指向子目录功能 我购买的webhostingpad空间虽然支持Parked Domains(域名停靠)功能,但停靠的域名只能指向网站根目录即主域名(如我的http://roadh.net),如果想将域名(如http://xyc.pp.ru)停靠到我在此空间上建立的子网站(通过Addon Domains功能建立)(如http://xya.in)上,单靠cpanel控制面板提供的功能是无法实现的,好在该空间支持.htaccess文件重写功能,现就将实现方法介绍如下: 1、首先在xyc.pp.ru的dns中将其cname到xya.in上; 2、编辑网站根目录下.htaccess文件,添加如下内容: RewriteEngine on #让xyc.pp.ru 访问xya.in目录xya.in网站 RewriteCond...
- 用.htaccess 文件帮你逃离Feedsky的不稳定 托管 Feed 有很多好处,但这些好处都要基于一个前提 —— Feed 托管商足够强健。自从使用 Feedsky 以来我不但没有体验到好处,积攒的怨念倒是3天3夜都散不完。我谁也不赖。要怪就怪自己当时贪图小利,明知道 Feedsky 不好,冲着话题广告还是头也不回地上了船。要说在...
- 实战讲解.htaccess文件之rewrite规则 .htaccess文件大家都不陌生吧。不认识的自己去搜一下……顺便告诉你,这是个好东西,值得一学。 似乎网上关于.htaccess编写方法的教程很有限,相信博主们也没有几个是自己写的吧? 我在这里就搜了几个常用规则,总结一下rewrite规则的用法。当然这只是.htaccess功能的一小部分,但是相当实用。 如果熟练掌握rewrite规则的编写,能够加强对网站URL的控制,对用户体验、SEO都十分有利。 注:所有规则来源于网络,我只作解释。 一、防盗链功能 1 2...
以上关联文章由 Yet Another Related Posts Plugin 提供支持。



















