配置Apache 虚拟主机支持二级域名的两种方法

Apache配置虚拟目录和二级域名http://skyfen.iteye.com/blog/515221

原文:http://wenku.baidu.com/view/0f292120a5e9856a56126018.html?re=view

方法一.利用Apache的mod_vhost_alias

引用:

   
#LoadModule vhost_alias_module modules/mod_vhost_alias.so 去掉前面的#
    <VirtualHost 127.0.0.1:80>
        ServerAdmin [email protected]
        ServerName x.com
        UseCanonicalName Off
        VirtualDocumentRoot /www/%1.1/%1.1%1.2/%0
    </VirtualHost>

这样的话域名:www.x.com指向/www/w/ww/www.x.com

域名:bottle.x.com指向/www/b/bo/bottle.x.com

而且新建站分只要建相应目录就可以了,比如新建new.x.com

那么只要创建目录/www/n/ne/new.x.com

我测试通过的案例

#add by lzk
<VirtualHost 127.0.0.1:8080>
ServerAdmin [email protected]
	ServerName testweb.com
	UseCanonicalName Off
	VirtualDocumentRoot E:/FSM-CMS2_WS_2008/WS_Projects/fsmcms_app/trunk/com.fsm.cms.war/war/publish/%1
</VirtualHost>
#需要开放目录权限,否则在访问的时候会提示无权限访问
<Directory "E:/FSM-CMS2_WS_2008/WS_Projects/fsmcms_app/trunk/com.fsm.cms.war/war/publish/">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
#
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
</Directory>
#add by lzk

如果输入test1.testweb.com,则对应E:/FSM-CMS2_WS_2008/WS_Projects/fsmcms_app/trunk/com.fsm.cms.war/war/publish/test1

如果输入test2.testweb.com,则对应E:/FSM-CMS2_WS_2008/WS_Projects/fsmcms_app/trunk/com.fsm.cms.war/war/publish/test2

说明:

%0=完整的域名ex:www.stksky.com

%1=www

%1+=www.stksky.com

%1.1=w

%1.2=w

%1.3=w

%2=stksky

%3+=stksky.com

%2.1=s

%2.2=t

%2.3=k

…….

%3=com

%3+=com

%3.1=c

…….

方法二.利用Apache的rewrite_module

引用:

首先,你的拥有一个有泛域名解析的顶级域名,例如:domain.com

其次,在httpd.conf中打开mod_rewrite

之后,在httpd.conf的最后,添加以下内容:

   
RewriteEngine on
    RewriteMap lowercase int:tolower
    RewriteMap vhost txt:/usr/local/etc/apache/vhost.map
    RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
    RewriteCond ${vhost:%1} ^(/.*)$

相关推荐