Automatically redirect to secure connection 'https'

Updated on 2026-10-11 18:00:47 672 visits

By default, when installing an SSL certificate, visits to the insecure version of your website (http://www.tudominio.com) are not automatically redirected to the secure version (https://www.tudominio.com).

Below, we show you several ways to configure the automatic redirection from HTTP (port 80) to HTTPS (port 443).

Using the '.htaccess' file in Apache

If your server uses Apache (in most cases), this is the fastest and simplest way:

  1. Connect via FTP to the datos/web directory and locate the .htaccess file. If it does not exist, create it.
  2. Add the following lines to the file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
  1. Save the changes.

Notice: Some CMS, such as WordPress, modify .htaccess automatically. In these cases, configure the redirection from your CMS panel or use another method from this guide.

Modifying Virtual Host (vhost) in Apache

If you have root access to your server:

  1. Connect via SSH.
  2. Display the Virtual Hosts configuration with: apache2ctl -S
  3. Identify the block corresponding to port 80 for your domain. For example:
*:80                   is a NameVirtualHost
         port 80 namevhost swpanel.com (/etc/apache2/sites-enabled/swpanel.com.conf:2)
                 alias www.swpanel.com
  1. Edit the indicated configuration file (example with nano):
nano /etc/apache2/sites-enabled/swpanel.com.conf
  1. Locate the port 80 block and add the redirection lines before </VirtualHost>:
<VirtualHost *:80>
    ServerName swpanel.com
    ServerAlias www.swpanel.com
    DocumentRoot '/var/www/swpanel.com/datos/web'

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</VirtualHost>
  1. Save the changes (Ctrl + X, then Y and Enter).
  2. Check the configuration:
apache2ctl -t

It should appear: Syntax OK.

  1. Reload Apache to apply the changes: /etc/init.d/apache2 graceful
/etc/init.d/apache2 graceful

Access your domain with http:// to verify that it automatically redirects to https://.

Modifying Virtual Host (vhost) in Nginx

If your server uses Nginx and you have root access:

  1. Connect via SSH.
  2. Access the Virtual Hosts directory:
cd /etc/nginx/sites-enabled  
ls
  1. Edit the file corresponding to the domain (example with nano):
nano /etc/nginx/sites-enabled/swpanel.com.conf
  1. Locate the port 80 block and add the redirection line:
server {
        listen 80;
        root '/var/www/swpanel.com/datos/web';
        index index.html index.php;
        server_name swpanel.com www.swpanel.com;

        return 301 https://$server_name$request_uri;
}
  1. Save the changes (Ctrl + X, then Y and Enter).
  2. Check the configuration:
nginx -t

Something like this should appear:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  1. Reload Nginx to apply the changes:
/etc/init.d/nginx reload

Access your domain with http:// to verify that it automatically redirects to https://.