Automatically redirect to secure connection 'https'.
By default, once your SSL certificate is installed, visits to the non-secure version ("http://www.tudominio.com") will not be automatically redirected to the secure version ("https://www.tudominio.com").
Here are several ways to configure the automatic redirection to "https", or in other words, from port 80 (http) to port 443 (https).
Using ".htaccess" file in Apache
This is the fastest and easiest way to do it if you are using an Apache server (most of them). We are going to configure the redirection by adding the lines that we will indicate below in the ".htaccess" file. To do this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Some content management systems (CMS), such as WordPress, modify the ".htaccess" file with their own settings. In these cases you will have to configure the redirection in their configuration panel or use one of the following methods.
Modifying Virtual Host (vhost) in Apache
If you have a Cloud with root access, you can choose to configure the redirection by modifying the Virtual Host in Apache:
-
Connect via SSH to your server.
-
Enter the following command to display the current Virtual Hosts configuration:
apache2ctl -S
-
From the block corresponding to port 80, copy the path to the file that corresponds to the web site to which you are going to apply the redirection. As an example, we will look at the domain "swhosting.com":
VirtualHost configuration:
*:80 is a NameVirtualHost
default server cm2019012345678.dnssw.net (/etc/apache2/sites-enabled/000-default.conf:17)
port 80 namevhost namevhost cm2019012345678.dnssw.net (/etc/apache2/sites-enabled/000-default.conf:17)
port 80 namevhost swhosting.com (/etc/apache2/sites-enabled/swhosting.com.conf:2)
alias www.swhosting.com
-
In this case, the path is:
/etc/apache2/sites-enabled/swhosting.com.conf
-
Edit the above configuration file. In this example we use the text editor nano, but you can use any other editor:
nano /etc/apache2/sites-enables/swhosting.com.conf
.
-
A first block corresponding to port 80 (http) appears:
<VirtualHost *:80>
ServerName swhosting.com
ServerAlias www.swhosting.com
DocumentRoot "/var/www/swhosting.com/data/web"
</VirtualHost>
- We add the following lines before the closing
</VirtualHost>
:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
- Resulting in the following form:
<VirtualHost *:80>
ServerName swhosting.com
ServerAlias www.swhosting.com
DocumentRoot "/var/www/swhosting.com/data/web"
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</VirtualHost>
-
We save the changes made. In the nano text editor, you must press Ctrl + X
to save and exit, Y
key to confirm that you want to overwrite, and Enter
key.
-
We check that the configuration is correct:
apache2ctl -t
You should see Syntax OK. If any error is indicated, review the previous steps.
-
Reload the Apache configuration to apply the changes:
/etc/init.d/apache2 graceful
.
Next, access your domain with the prefix "http://" to check if it automatically redirects to "https://".
Translated with www.DeepL.com/Translator (free version)
Modifying Virtual Host (vhost) in Nginx
If you have a Cloud with root access, you can choose to configure the redirection by modifying the Virtual Host in Nginx:
-
Connect via SSH to your server.
-
Access the directory where the Virtual Host configuration file is located and list the contents. Usually:
cd /etc/nginx/sites-enabled
.
ls
-
We edit the file that corresponds to the Virtual Host for which we are going to apply the redirection to "https". In this example we use the text editor nano, but you can use any other:
nano /etc/nginx/sites-enabled/swhosting.com.conf
.
-
A first block corresponding to port 80 (http) appears:
server {
listen 80;
root "/var/www/swhosting.com/data/web";
index index.html index.php;
server_name swhosting.com www.swhosting.com;
}
- We add the following line:
return 301 https://$server_name$request_uri;
server {
listen 80;
root "/var/www/swhosting.com/data/web";
index index.html index.php;
server_name swhosting.com www.swhosting.com;
return 301 https://$server_name$request_uri;
}
-
We save the changes made. In the text editor nano, you must press Ctrl + X
to save and exit, Y
key to confirm that you want to overwrite, and Enter
key.
-
We check that the configuration is correct:
nginx -t
If everything is correct, you will see something similar to:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If any error is indicated, review the previous steps.
- Reload the Nginx configuration to apply the changes:
/etc/init.d/nginx reload
Next, access your domain with the prefix "http://" to check if it automatically redirects to "https://".