13

I am interested in, if I can have vhosts on apache with domain names like: http://something.com/something or http://{server-ip-address-here}/something ?

I am using Apache 2.2.20 on Ubuntu Server, thats my home server and I am testing some stuff here, I dont have any DNS server here and what I have is only public IP address and a domain name attached to it from open dns service.

So, what did I do:

  1. I have created new file "demo" in /etc/apache2/sites-available
  2. I put there this (actually it is copied with modifications from default file):

    <VirtualHost *:80>
       ServerAdmin webmaster@localhost
       ServerName  {mydomain-here}/demo/
       DocumentRoot /vhosts/demo
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /vhosts/demo/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>
    
    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
    
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
    </VirtualHost>
    

  3. Created symlink in /etc/apache2/sites-enabled/ which points to /etc/apache2/sites-available/demo

  4. Created /vhosts/demo/index.html file.

And now what i get is that when I go to {my-domain} I go to vhost which I have created, but problem is that server points me there in ANY case, not only {my-domain}/demo what I want.

In conclusion, I want that I can create different virtual hosts and attach them to different URL's which will have same base-url, for example www.mydomain.com/vhost1, www.mydomain.com/vhost2 etc..

Is it possible? Thanks :)

Jibla
  • 813
  • 2
  • 8
  • 12

1 Answers1

16

To start, the reason why it goes there is ANY case is cause you have you have a *:80 setting for your virtual host, so if nothing matches the request it uses the first virtual host entry

If I understand what you are trying to do it appears like you might just want to alias each 'virtual host'

What you are trying to do isn't quite a virtual host (at least what a virtual host is supposed to do), but you might be able to accomplish it by using alias feature of apache

Alias /vhost1 /whatever/folder/your/vhost1/site/is/at
Alias /vhost2 /whatever/folder/your/vhost2/site/is/at

So now whatever domain you use e.g. http://whatever.com/vhost1 or http://whatever.com/vhost2 The both of em will appear as separate sites

jeffchong07
  • 552
  • 6
  • 13
  • Thanks :) I created Aliases and I got exactly that result, which I wanted! But, for interest: It looks like that its not possible to make virtual hosts that way, is it? – Jibla Feb 29 '12 at 20:45
  • 1
    I don't think so, I've never used it like that before. Generally you use it when you need one server to be able to server files for multiple domains/ips e.g. www.vhost1.com and www.vhost2.com and using the virtual host is how the server knows what files to server www.vhost1.com and www.vhost2.com – jeffchong07 Feb 29 '12 at 20:53
  • in your case, there is only one domain so the virtual host isn't necessarily useful to differentiate your sites – jeffchong07 Feb 29 '12 at 20:54
  • Thank you dude :) I got what I wanted to get with aliases ;) – Jibla Mar 01 '12 at 00:34
  • 2
    Hey guys, where exactly you are suppose to put above lines? – Super Engineer Jul 14 '13 at 10:31
  • depends how you have your configurations setup. If you wanna do it server wide, you could add it in the conf/httpd.conf file, or you can put it in your conf.d/vhost.conf (or whatever you call your extra virtual host config file) in the specific virtual host you are add these rules too – jeffchong07 Jul 15 '13 at 16:18