diff --git a/templates/named_vhost.conf.erb b/templates/named_vhost.conf.erb
index 51321bf881d2788191cb2ac1381fe09f30dce208..66aa99e8a344b5e81a282ec528307fb6aef5d81a 100644
--- a/templates/named_vhost.conf.erb
+++ b/templates/named_vhost.conf.erb
@@ -1,5 +1,5 @@
 <%
-    # Copyright (C) 2014-2022 Thomas Bellman.
+    # Copyright (C) 2014-2024 Thomas Bellman.
     # Licensed under the GNU LGPL v3+; see the README file for more information.
 
     httpd_version = scope.lookupvar('apache::standard_version')
@@ -51,16 +51,37 @@
 	server_names = [@servernames].flatten
     end
     server_addrs.sort!.uniq!
--%>
-<%
-##    cfg = []
-##    cfg << 'ServerName ' + server_names[0]
-##    cfg += server_names[1..-1].collect { |aliasname|
-##	'ServerAlias ' + aliasname
-##    }
-##    if @documentroot && @documentroot != ''
-##	cfg << 'DocumentRoot ' + @documentroot
-##    end
+
+    # Config lines inside the <VirtualHost> container
+    vhostcfg = []
+
+    vhostcfg << 'ServerName ' + server_names[0]
+    vhostcfg += server_names[1..-1].collect { |aliasname|
+	'ServerAlias ' + aliasname
+    }
+
+    if @certname && @certname != ''
+	certdir = scope.lookupvar('x509certs::pki_certdir')
+	keydir  = scope.lookupvar('x509certs::pki_keydir')
+	certfile  = File.join(certdir, @certname + '-cert.pem')
+	keyfile   = File.join(keydir, @certname + '-key.pem')
+	chainfile = File.join(certdir, @certname + '-chain.pem')
+	vhostcfg += [
+	    '',
+	    'SSLEngine on',
+	    'SSLCertificateFile ' + certfile,
+	    'SSLCertificateKeyFile ' + keyfile,
+	    'SSLCertificateChainFile ' + chainfile,
+	]
+    end
+
+    if @documentroot && @documentroot != ''
+	vhostcfg += [ '', 'DocumentRoot  ' + @documentroot ]
+    end
+
+    if not @config.empty?
+	vhostcfg += [ '', @config ].flatten
+    end
 -%>
 <% if (httpd_version  and  httpd_version != ''  and
        scope.function_versioncmp([httpd_version, '2.3.11']) < 0)
@@ -71,24 +92,6 @@ NameVirtualHost <%= addr %>
 <% end -%>
 <VirtualHost <%= server_addrs.join(' ') %>>
 
-    ServerName <%= server_names[0] %>
-<% server_names[1..-1].each do |aliasname| -%>
-    ServerAlias <%= aliasname %>
-<% end -%>
-<% if @certname && @certname != ''
-    certdir = scope.lookupvar('x509certs::pki_certdir')
-    keydir  = scope.lookupvar('x509certs::pki_keydir')
--%>
-    SSLEngine on
-    SSLCertificateFile <%= certdir %>/<%= @certname %>-cert.pem
-    SSLCertificateKeyFile <%= keydir %>/<%= @certname %>-key.pem
-    SSLCertificateChainFile <%= certdir %>/<%= @certname %>-chain.pem
-
-<% end -%>
-<% if @documentroot && @documentroot != '' -%>
-    DocumentRoot  <%= @documentroot %>
-<% end -%>
-
-<%= [@config].flatten.join("\n").gsub(/^./, '    \&') %>
+<%= vhostcfg.join("\n").gsub(/^./, '    \&') %>
 
 </VirtualHost>