2011-09-07 107 views
4

我想创建一个注册表单来创建子域(但在本地主机上),但我遇到了一些问题。我知道如何创建子域,在虚拟主机例如写这些:本地主机上的子域

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    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”在所有这些排列中对其进行了尝试,但这些都不适合我。 我曾经想过,在注册时,我在虚拟主机中添加了一个新的数据块,并提供了最佳数据,但我认为这不是很安全/可行,或者是最好的方法。

希望有人能帮助我!

在此先感谢!

回答

2

你可以尝试一个rewriterule,它将一个子域转换成一个文件夹。

例如,mystuff.localhost变为本地主机/的MyStuff

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

+0

重写规则进入htaccess的?我从来没有在本地主机上试过htaccess,所以它放在索引文件所在的同一个文件夹中? – matthew3r

+0

检查[mod_rewrite上的apache文档](http://httpd.apache.org/docs/current/mod/mod_rewrite。html):rewriterules进入包含在conf.d中的文件。 – Konerak

2

尝试在serveralias增加另一个域:

ServerAlias something.localhost other.localhost 
+0

这对我来说很合适,但我可以手动完成,或者不是?我想在注册时执行此操作,但我并不真正知道这是一种以编程方式放入其他.localhost的安全解决方案,或者我可以这样做。 - 我注册的意思是,用户可以为自己更改一个子域名,并达到目的。 – matthew3r

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走了,换了工作域

+0

我想知道域名来自哪里,但我认为它代表“本地虚拟主机”。尼斯。对于像我这样的人,想知道它是否是一些标准的操作系统:[不,只是一个注册的域名](http://network-tools.com/nslook/Default.asp?domain=lvho.st&type=1&server=ns2。 nic.st&类= 1&端口= 53&超时= 5000)。它[不支持IPv6](http://network-tools.com/nslook/Default.asp?domain=lvho.st&type=28&server=ns2.nic.st&class=1&port=53&timeout=5000)(但)但我猜测这对localhost无关紧要。 – Arjan

+0

是的,有些灵魂为大众注册:) – jpillora

+0

太棒了!我怎么从来不知道这件事? –

9

lvh.me也是一个别名为localhost。 Jamo对于lvho.st来说是完美的测试子域名。

+0

这现在只适用于lvh.me,而不适用于lvho.st. lvh.me似乎支持子子域和子子子域等,这很方便(例如:http://foo.bar.bat.bongo.lvh.me) –

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>