求aphache配置本地localhost任意二级域名的方法。

时间:2022-09-17 00:22:33
我将host文件指向为
127.0.0.1       localhost
127.0.0.1       localhost.com (前面到底要不要加www.?)

本地网站根目录为
d:\phpnow\htdocs\

我想实现
http://www.localhost.com 指向 d:\phpnow\htdocs\
http://xxx.localhost.com 指向 d:\phpnow\htdocs\xxx (xxx为任意文件夹)

怎么写该apache的配置文件达到以上要求?

=====================================================
网上找到一个方法,可是无效
APACHE二级域名的设置
一般我们进行本地测试时,可能都是用的localhost,但为了能和服务器环境同步,于是尽可能的用域名来调试,方法就是修改HOST文件。
将欲调试的域名解析到本地的127.0.0.1,但遇到多个二级域名时,就没辙了,
这里有一个好的方法来解决这个问题:利用APACHE强大的正则功能。
如:网站有多个二级域名www.your.com,diy.your.com....
网站的根目录为 /wwwRoot/
在根目录下新建目录www,diy...
将www.your.com的内容放入www,diy.your.com放入diy
修改CONF配置文件,添加一条虚拟主机信息
如下:
<VirtualHost *>
ServerAlias www.your.com
ServerName www.your.com
DocumentRoot d:/webserver/wwwRoot/
RewriteEngine  on
RewriteCond    %{HTTP_HOST}  ^your.com
RewriteRule    ^(.*)$        /www/$1 [L]
RewriteCond    %{HTTP_HOST}  ^www.*
RewriteRule    ^(.*)$        /www/$1 [L]
RewriteCond    %{HTTP_HOST}  ^(.*)\.your\.com
RewriteRule    ^(.*)$        /%1/$1 [L]
</VirtualHost>

3 个解决方案

#1



不是在httpd.conf这里写吗?
D:\phpnow\Apache-20\conf\目录下有
httpd.conf (我按那方法改了这个)
httpd-win.conf
这么两个文件,我的电脑是xp系统,应该改httpd-win.conf ?

#2


C:\xampp\apache\conf\extra\httpd-vhosts.conf
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host.example.com
##    DocumentRoot /www/docs/dummy-host.example.com
##    ServerName dummy-host.example.com
##    ServerAlias www.dummy-host.example.com
##    ErrorLog @rel_logfiledir@/dummy-host.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host.example.com-access_log common
##</VirtualHost>

##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
##    DocumentRoot /www/docs/dummy-host2.example.com
##    ServerName dummy-host2.example.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
##</VirtualHost>


<VirtualHost *:80>
 ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace
    ServerName 127.0.0.1
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>
<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/panda/www/
    ServerName panda
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot E:/workspace/nikonReg20100607/
    ServerName nikon_reg.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>


<VirtualHost *:80>
 ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_front/app/webroot
    ServerName kon-front.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_admin/app/webroot
    ServerName nikon-admin.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikonReg20100607
    ServerName nikon_reg.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/joomla
    ServerName joomla.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_front_20100205/nikon_front/app/webroot
    ServerName nikon-front.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot c:/xampp/phpMyAdmin
    ServerName phpmyadmin.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/panda/www/
    ServerName panda.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/kadoya/
    ServerName kadoya.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/code1.0/kadoya
    ServerName kadoya1.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/aiya/
    ServerName aiya.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/oisc/
    ServerName oisc.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_front/app/webroot
    ServerName nikon.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_contest/
    ServerName nikon-contest.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/rijing1.0/
    ServerName www.rijing.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

#3



127.0.0.1 www.xxx.com
127.0.0.1       这个地方就是上面文件的ServerName

#1



不是在httpd.conf这里写吗?
D:\phpnow\Apache-20\conf\目录下有
httpd.conf (我按那方法改了这个)
httpd-win.conf
这么两个文件,我的电脑是xp系统,应该改httpd-win.conf ?

#2


C:\xampp\apache\conf\extra\httpd-vhosts.conf
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host.example.com
##    DocumentRoot /www/docs/dummy-host.example.com
##    ServerName dummy-host.example.com
##    ServerAlias www.dummy-host.example.com
##    ErrorLog @rel_logfiledir@/dummy-host.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host.example.com-access_log common
##</VirtualHost>

##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
##    DocumentRoot /www/docs/dummy-host2.example.com
##    ServerName dummy-host2.example.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
##</VirtualHost>


<VirtualHost *:80>
 ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace
    ServerName 127.0.0.1
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>
<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/panda/www/
    ServerName panda
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot E:/workspace/nikonReg20100607/
    ServerName nikon_reg.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>


<VirtualHost *:80>
 ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_front/app/webroot
    ServerName kon-front.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_admin/app/webroot
    ServerName nikon-admin.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikonReg20100607
    ServerName nikon_reg.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/joomla
    ServerName joomla.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_front_20100205/nikon_front/app/webroot
    ServerName nikon-front.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot c:/xampp/phpMyAdmin
    ServerName phpmyadmin.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/panda/www/
    ServerName panda.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/kadoya/
    ServerName kadoya.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/code1.0/kadoya
    ServerName kadoya1.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/aiya/
    ServerName aiya.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/oisc/
    ServerName oisc.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_front/app/webroot
    ServerName nikon.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/nikon_contest/
    ServerName nikon-contest.keynes-soft.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot e:/workspace/rijing1.0/
    ServerName www.rijing.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

#3



127.0.0.1 www.xxx.com
127.0.0.1       这个地方就是上面文件的ServerName