# Apache VirtualHosts

**URL:** <https://ifnotnil.com/t/apache-virtualhosts/1558>\
**Category:** Web\
**Created:** [22 February 2021 23:48 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558 "2021-02-22T23:48:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [22 February 2021 23:48 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558/1 "2021-02-22T23:48:13Z")

</div>

I know there aren’t many apache folks here (if any?) but figured I might try asking anyway…

When creating multiple sites on one host, instructions say to create a .conf file for each URL. e.g.  
`example.com.conf` containing:

```auto
<VirtualHost *:80>
    DocumentRoot "/www/example.com"
    ServerName example.com
    SeverAlias www.example.com
</VirtualHost>

```

and  
`example.org.conf` containing:

```auto
<VirtualHost *:80>
    DocumentRoot "/www/example.org"
    ServerName example.org
    ServerAlias www.example.org
</VirtualHost>

```

even though you can have the entries in a single file. (I think) like this:  
`example.conf` containing:

```auto
<VirtualHost *:80>
    DocumentRoot "/www/example.com"
    ServerName example.com
    ServerAlias www.example.com
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/www/example.org"
    ServerName example.org
    ServerAlias www.example.org
</VirtualHost>

```

What’s the advantage of having 2 files over putting both settings within a single  
`example.conf` file?

---

<div class="post-metadata">

**Author:** ![swampcat](https://ifnotnil.com/user_avatar/ifnotnil.com/swampcat/32/78_2.png) [@swampcat](https://ifnotnil.com/u/swampcat)\
**Post date:** [23 February 2021 02:47 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558/2 "2021-02-23T02:47:53Z")

</div>

I find it useful to keep them in different files for a couple of reasons:

- Temporary web sites can be easily added or torn down without touching my production sites. For example, I was playing with the Omeka content management system. By keeping it in a separate file, I don’t have to determine what belongs to Omeka (e.g., Directory directives) and what belongs to my production site.

- You can use the “a2en…” and “a2dis…” tools to change the configuration. Using the example above, I could `a2dissite omeka` on my Ubuntu based system and it will remove the link in `/etc/apache2/sites-enabled`. I could put it back with `a2ensite omeka` later. (You also have `a2enmod` and `a2dismod` for modules, etc.)

The logical separation enforced by using different files, though, mostly protects me from my future self that has forgotten half of what I did. And I need all the help that I can get for that.

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [23 February 2021 05:39 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558/3 "2021-02-23T05:39:38Z")

</div>

Thank you @swampcat , that makes perfect sense!

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [23 February 2021 19:54 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558/4 "2021-02-23T19:54:45Z")

</div>

Do you know if it’s permissible to have a DNS entry that has a wildcard extension?  
i.e. if I own [example.com](http://example.com), [example.net](http://example.net), [example.org](http://example.org), example.ca, etc. can I have an A record that’s:  
`example.*` instead of using CNAMEs for each possible suffix?

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [23 February 2021 20:18 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558/5 "2021-02-23T20:18:50Z")

</div>

If not, then can that sort of wildcard be used in the `Virtualhosts` entry? .e.g

```auto
<VirtualHost *:80>
    DocumentRoot "/www/example.com"
    ServerName example.*
    ServerAlias www.example.*
</VirtualHost>

```

---

<div class="post-metadata">

**Author:** ![swampcat](https://ifnotnil.com/user_avatar/ifnotnil.com/swampcat/32/78_2.png) [@swampcat](https://ifnotnil.com/u/swampcat)\
**Post date:** [24 February 2021 14:06 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558/6 "2021-02-24T14:06:09Z")

</div>

I don’t know of any way to have a wildcard. I’ll admit that I don’t have an in-depth knowledge of all the options.

My first thought would be to define a virtual host for each variant. One site will have all of the definitions for your site and all the others will have a `Redirect` directive to point to the primary. An alternative would be to use rewrite instead so the domain name doesn’t change in the visitor’s address bar.

This is one case where I would put all the virtual host definitions in one file since they are essentially a single unit that you would want to control as one and group together for ease of management.

---

<div class="post-metadata">

**Author:** ![Tim](https://ifnotnil.com/user_avatar/ifnotnil.com/tim/32/13_2.png) [@Tim](https://ifnotnil.com/u/Tim)\
**Post date:** [24 February 2021 17:41 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558/7 "2021-02-24T17:41:52Z")

</div>

I feel kind of awkward as you’re interested in Apache, but I thought you might want to know that nginx will actually do what you want:

> The parameter to `server_name` can be a full (exact) name, a wildcard, or a regular expression.

I really don’t mean to be rude and derail, I just thought it was worth your time!

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [24 February 2021 18:40 UTC](https://ifnotnil.com/t/apache-virtualhosts/1558/8 "2021-02-24T18:40:43Z")

</div>

No worries. I’m looking at Nginx as well.

From what I’m reading, it seems there cannot be wildcards in an A record, but there can on a CNAME, but only as the first part. .e.g.  
`*.example.com` is ok, but not `example.*`

Similarly, in the VirtualHosts section, you can’t have any wildcards in the ServerName parameter, but you can have them anywhere in the ServerAlias, it seems.

```auto
<VirtualHost *:80>
    DocumentRoot "/www/example.com"
    ServerName example.com
    ServerAlias *.example.*
</VirtualHost>

```

Seems to work with [example.com](http://example.com), [example.net](http://example.net), [test.example.com](http://test.example.com), etc, as long as there’s an A or CNAME record for each…
