diff --git a/http/nginx/vhost.tpl.php b/http/nginx/vhost.tpl.php
index 3c326fc..37f5ed3 100644
--- a/http/nginx/vhost.tpl.php
+++ b/http/nginx/vhost.tpl.php
@@ -1,7 +1,7 @@
 server {
    limit_conn   gulag 10; # like mod_evasive - this allows max 10 simultaneous connections from one IP address
    listen       <?php print $ip_address . ':' . $http_port; ?>;
-   server_name  <?php print $this->uri; ?><?php if (!$this->redirection && is_array($this->aliases)) : foreach ($this->aliases as $alias_url) : if (trim($alias_url)) : ?> <?php print $alias_url; ?><?php endif; endforeach; endif; ?>;
+   server_name  <?php print $this->uri . ' ' . implode(' ', $this->aliases); ?>
    root         <?php print $this->root; ?>;
    index        index.php index.html;
 <?php 
@@ -12,6 +12,23 @@ server {
       print '   include      ' . $server->include_path . '/nginx_simple_include.conf';
     }
 ?>;
+
+<?php
+  if ($this->redirection || $ssl_redirection) {
+    if ($ssl_redirection && !$this->redirection) {
+      // redirect aliases in non-ssl to the same alias on ssl.
+      print "\n  rewrite ^/(.*)$  https://\$http_host/$1 permanent;\n";
+    }
+    elseif ($ssl_redirection && $this->redirection) {
+      // redirect all aliases + main uri to the main https uri.
+      print "\n  rewrite ^/(.*)$  https://{$this->uri}/$1 permanent;\n";
+    }
+    elseif (!$ssl_redirection && $this->redirection) {
+      // Redirect all aliases to the main http url.
+      print "\n if (\$http_host !~* \"{$this->uri}\" ) { \n  rewrite ^/(.*)$  http://{$this->uri}/$1 permanent;\n}\n";
+    }
+  }
+ ?>
 }
 
 <?php
diff --git a/http/nginx_ssl/vhost_ssl.tpl.php b/http/nginx_ssl/vhost_ssl.tpl.php
index 325ae47..8852b0a 100644
--- a/http/nginx_ssl/vhost_ssl.tpl.php
+++ b/http/nginx_ssl/vhost_ssl.tpl.php
@@ -4,7 +4,7 @@
 server {
    limit_conn   gulag 10; # like mod_evasive - this allows max 10 simultaneous connections from one IP address
    listen       <?php print "{$ip_address}:{$http_ssl_port}"; ?>;
-   server_name  <?php print $this->uri; ?> <?php if (!$this->redirection && is_array($this->aliases)) : foreach ($this->aliases as $alias_url) : if (trim($alias_url)) : ?> <?php $alias_url = "." . $alias_url; ?> <?php print $alias_url; ?> <?php endif; endforeach; endif; ?>;
+   server_name  <?php print $this->uri . ' ' . implode(' ', $this->aliases); ?>;
    root         <?php print $this->root; ?>;
    index        index.php index.html;
    ssl                         on;
@@ -22,29 +22,21 @@ server {
       print '   include      ' . $server->include_path . '/nginx_simple_include.conf';
     }
 ?>;
-}
 
-<?php endif; ?>
 
-<?php 
-   if ($this->ssl_enabled != 2) :
-     // Generate the standard virtual host too.
-     include('http/nginx/vhost.tpl.php');
+<?php
+  if ($this->redirection) {
+    // Redirect all aliases to the main http url.
+    print "\n if (\$http_host !~* \"{$this->uri}\" ) { \n  rewrite ^/(.*)$  https://{$this->uri}/$1 permanent;\n}\n";
+  }
+ ?>
 
-   else :
-     // Generate a virtual host that redirects all HTTP traffic to https.
-?>
 
-server {
-  listen       <?php print $ip_address . ':' . $http_port; ?>;
-  server_name  <?php print $this->uri; ?> <?php if (!$this->redirection && is_array($this->aliases)) : foreach ($this->aliases as $alias_url) : if (trim($alias_url)) : ?> <?php $alias_url = "." . $alias_url; ?> <?php print $alias_url; ?> <?php endif; endforeach; endif; ?>;
-  root         <?php print $this->root; ?>;
-  index        index.php index.html;
-  location / {
-     root   /var/www/nginx-default;
-     index  index.html index.htm;
-     rewrite ^/(.*)$  <?php print $ssl_redirect_url ?>/$1 permanent;
-  }
 }
 
 <?php endif; ?>
+
+<?php 
+   // Generate the standard virtual host too.
+   include('http/nginx/vhost.tpl.php');
+?>
