Subdomain on localhost

2019-02-04 03:11发布

问题:

I would like to create a registration form which creates subdomains (yet on localhost), but I have got some problem. I know how to create subdomains, writing for example these in 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>

And put this line in hosts:

127.0.0.0              something.localhost

It's working, but what I want is that when I register a new subdomain (for example: other), then when I try to open other.localhost, then it opens the specified folder (../www/other/). I tried it in vhosts with "ServerName *.localhost", "ServerName localhost", "ServerAlias *.localhost", "ServerAlias localhost", and in hosts "127.0.0.1 *.localhost" with all of the permutation of these, but neither of these worked for me. I have thinked about it, that on registration I put a new block in vhosts, with the optimal data, but I don't think it's very safe/feasible/or the best way to do.

Hope someone can help me!

Thanks in advance!

回答1:

You could try a rewriterule, which converts a subdomain into a folder.

For example, mystuff.localhost becomes localhost/mystuff

otherthing.localhost/some/dir/here becomes localhost/otherthing/some/dir/here



回答2:

http://*.lvh.me/ is an alias to localhost. Perfect for testing subdomains.

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

Edit: 2016/07: lvho.st went away, swapped for working domain



回答3:

lvh.me is also an alias to localhost. Perfect for testing subdomains as Jamo say for lvho.st.



回答4:

try adding another domain in serveralias:

ServerAlias something.localhost other.localhost


回答5:

You should create a dinamically configured mass virtual hosting.

This allows you to define a single virtual host entry to handle all incoming requests for different hosts and delegate each request to the corresponding directory.

This way you avoid having to configure an new virtual host for each new domain you add. Instead you just create the directory in the file system and everything just works.

First you enable the mod_vhost_alias extension:

sudo a2enmod vhost_alias

Then configure your single virtual host entry like this:

# 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>