· Tutorial ·

Automatically redirect to secure connection 'https'.

By default, when you install an SSL certificate, visits to the unsecured 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 automatic redirection from HTTP (port 80) to HTTPS (port 443).

Using the “.htaccess” file in Apache

If your server uses Apache (which is usually the case), this is the quickest and easiest method:

  1. Connect via FTP to the data/web directory and locate the .htaccess file. If it doesn’t 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 your changes.

Note: Some CMS platforms, such as WordPress, automatically modify .htaccess. In these cases, configure the redirect from your CMS dashboard or use another method described in 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 specified configuration file (example using nano):
nano /etc/apache2/sites-enabled/swpanel.com.conf
  1. Find the section for port 80 and add the redirect 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 settings:
apache2ctl -t

The following should appear: Syntax OK.

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

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

Modifying the Virtual Host (vhost) in Nginx

If your server uses Nginx and you have root access:

  1. Connect via SSH.
  2. Navigate to the Virtual Hosts directory:
cd /etc/nginx/sites-enabled  
ls
  1. Edit the file for the domain (example using nano):
nano /etc/nginx/sites-enabled/swpanel.com.conf

  1. Find the block for port 80 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 settings:
nginx -t

It should look something like:

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

Go to your domain at http:// to verify that it automatically redirects to https://.

i
Email send icon