简体   繁体   中英

Apache Virtual Host using mod_vhost_alias

I am trying to set up my apache module to dynamically direct all requests to a specific folder and then match the name to a folder of the same name.

To do this I set the following in my 000-default.conf file in the sites-available folder.

UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2

This worked great.

Then I wanted to setup a couple of different domains to not point to the example folder, but somewhere else, so I added a couple of these before the VirtualDocumentRoot line:

<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>

However, now the dynamic pointing does not work anymore and all the URL's are redirected to the first -> VirtualDocumentRoot location.

Can someone please indicate to me what I am doing wrong?

Full Code Example In apache2/sites-available/000-default.conf :

<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>

<VirtualHost *:80>
ServerName sub2.example.com
VirtualDocumentRoot /var/www/sub2.example.com
</VirtualHost>

<VirtualHost *:80>
ServerName sub3.example.com
VirtualDocumentRoot /var/www/sub3.example.com
</VirtualHost>

UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2

Do not use VirtualDocumentRoot for simple Virtualhosts, use only DocumentRoot .

VirtualDocumentRoot defines the mass-virtualhost catch-all, and by definition you can only have one mass-virtualhost (else how could apache knows which VH a given hostname should match).

Edit:

Now you need some other changes: - ensure you have NameVirtualHost *:80 somewhere in apache configuration (unless you use Apache 2.4). - Move the Mass-Virtualhost as first , so it will become the default virtualhost. The default virtualhost is used when the request host name does not match any ServerName directive. (You can check the default VH by running apache with -S option).

I have figured out how to do this, and decided to post the solution here for anyone else sitting with a similar problem:

SO to setup apache2, using mod_vhost_alias to have all domains point to a generic folder with the same name, but specific domains to point elsewhere, this is what you need to do.

In your 000-default.conf site config file, write the following code:

UseCanonicalName Off

Then add the following block for each specific domain you want to point to a specific folder, replacing example.com with your domain name:

<VirtualHost *:80>
 ServerName example.com
 ServerAlias www.*
 DocumentRoot path/to/your/folder
</VirtualHost>

Then add the next block to point all other generic domains to a generic folder:

<VirtualHost *:80>
 ServerName vhosts.fqdn
 ServerAlias www.*
 VirtualDocumentRoot path/to/your/folder/%2+
</VirtualHost>

<VirtualHost *:80>
 ServerName vhosts.fqdn
 ServerAlias *
 VirtualDocumentRoot path/to/your/folder/%1+
</VirtualHost>

The first block will direct all domains, starting with www. to a folder matching the name after the www. The second block is to direct the same domains, when no www. is specified, to the same folder.

For more information on the dynamic mass virtual host options to use in the document root, go to: http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM