localhost上的子域

时间:2011-09-07 14:33:38

标签: apache virtualhost

我想创建一个创建子域名的注册表单(但在localhost上),但我遇到了一些问题。我知道如何创建子域,例如在vhosts中编写这些子域:

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.hleclerc-PC.ingenidev
    DocumentRoot "C:/wamp/www/something/"
    ServerName localhost
    ServerAlias something.localhost
    ErrorLog "logs/error.log"
    CustomLog "logs/access.log" common
</VirtualHost>

并将此行放在主持人中:

127.0.0.0              something.localhost

它正在工作,但我想要的是当我注册一个新的子域(例如:other)时,当我尝试打开other.localhost时,它会打开指定的文件夹(../www/other/) 。 我在vhosts中尝试了“ServerName * .localhost”,“ServerName localhost”,“ServerAlias * .localhost”,“ServerAlias localhost”,以及主机“127.0.0.1 * .localhost”中的所有这些排列,但是这些都不适合我。 我已经考虑过了,在注册时我在vhosts中添加了一个新块,并提供了最佳数据,但我认为它不是非常安全/可行/或者是最好的方法。

希望有人可以帮助我!

提前致谢!

5 个答案:

答案 0 :(得分:27)

http://*.lvh.me/localhost的别名。非常适合测试子域。

$ host lvh.me
lvh.me has address 127.0.0.1
$ host foo.lvh.me
foo.lvh.me has address 127.0.0.1

编辑:2016/07:lvho.st离开,换掉工作域

答案 1 :(得分:9)

lvh.me也是localhost的别名。非常适合以Jamolvho.st所说的子域进行测试。

答案 2 :(得分:2)

您可以尝试重写,将子域转换为文件夹。

例如,mystuff.localhost成为localhost / mystuff

otherthing.localhost/some/dir/here变为localhost/otherthing/some/dir/here

答案 3 :(得分:2)

尝试在serveralias中添加另一个域:

ServerAlias something.localhost other.localhost

答案 4 :(得分:0)

您应该创建dinamically configured mass virtual hosting

这允许您定义单个虚拟主机条目来处理不同主机的所有传入请求,并将每个请求委派给相应的目录。

这样您就可以避免为添加的每个新域配置新的虚拟主机。相反,你只需在文件系统中创建目录,一切正常。

首先启用mod_vhost_alias扩展名:

sudo a2enmod vhost_alias

然后配置您的单个虚拟主机条目,如下所示:

# get the server name from the Host: header
UseCanonicalName Off

# this log format can be split per-virtual-host based on the first field
# this makes it easy to grep
LogFormat "%V %h %l %u %t \"%r\" %s %b" combined

<VirtualHost *:80>

    # the %0 is replaced by the host name on each request
    # if you want to use only part of the domain as directory
    # you would have to change the %0 for a %1 or %2 depending
    # on which part of the domain you want to take.
    VirtualDocumentRoot /your-main-directory/%0

    # configure your main directory anyway you want it
    <Directory /your-main-directory>
        Options Indexes FollowSymLinks -MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    #I have a single cgi-bin directory, but there is also a VirtualScriptAlias
    # available in case you need it.
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>