【问题标题】:Subdomain on localhost本地主机上的子域
【发布时间】:2011-11-12 05:39:36
【问题描述】:

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

<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.localhost 时,它会打开指定的文件夹(../www/other/) . 我在具有“ServerName *.localhost”、“ServerName localhost”、“ServerAlias *.localhost”、“ServerAlias localhost”和主机“127.0.0.1 *.localhost”的虚拟主机中尝试了它的所有排列,但是这些都不适合我。 我已经考虑过了,在注册时我在虚拟主机中放置了一个新块,具有最佳数据,但我认为这不是非常安全/可行/或最好的方法。

希望有人可以帮助我!

提前致谢!

【问题讨论】:

    标签: apache virtualhost


    【解决方案1】:

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

    例如,mystuff.localhost 变成 localhost/mystuff

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

    【讨论】:

    • RewriteRule 进入 htaccess?我从未在 localhost 上尝试过 htaccess,所以它位于索引文件所在的同一文件夹中?
    • 检查apache documentation on mod_rewrite: rewriterules 进入包含在您的 conf.d 中的文件中。
    【解决方案2】:

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

    ServerAlias something.localhost other.localhost
    

    【讨论】:

    • 这对我来说是正确的,但我可以手动完成,还是不能?我想在注册时这样做,但我真的不知道以编程方式放入 other.localhost 是否安全的解决方案,或者我可以这样做。 - 我在注册时的意思是用户可以为自己更改一个子域名,并达到它。
    【解决方案3】:

    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 消失,换成工作域

    【讨论】:

    • 我想知道域名来自哪里,但我认为它代表“本地虚拟主机”。好的。对于像我这样想知道这是否是一些标准操作系统的人:no, simply a registered domain。它有no support for IPv6(还),但我想这对本地主机无关紧要。
    • 是的,某个善良的灵魂为大众注册了它:)
    • 太棒了!我怎么从来不知道这件事?
    【解决方案4】:

    lvh.me 也是 localhost 的别名。非常适合测试子域,例如 Jamo,例如 lvho.st

    【讨论】:

    • 现在这仅适用于 lvh.me,而 not 适用于 lvho.st。 lvh.me 好像支持子子域,子子子域等,好用(例如:foo.bar.bat.bongo.lvh.me
    【解决方案5】:

    您应该创建一个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>
    

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      相关资源
      最近更新 更多