Redirect automatically to secure connection 'https'
By default, once your SSL certificate is installed, visits to the unsecured version ("http://www.yourdomain.com") will not be automatically redirected to the secure version ("https://www.yourdomain.com ").
We indicate 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. We are going to configure the redirection adding the lines that we will indicate in the file ".htaccess". For it:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
```
Warning: Some content managers (CMS) such as WordPress modify the ".htaccess" file with their own configuration. In these cases, you must configure the redirection in your 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 show the current configuration of Virtual Hosts:
apache2ctl -S
-
From the block corresponding to port 80, copy the path of the file that corresponds to the web to which you are going to apply the redirection. As an example, we will look at the domain "swhosting.com":
*:80 is a NameVirtualHost
default server cm2019012345678.dnssw.net (/etc/apache2/sites-enabled/000-default.conf:17)
port 80 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 route is:
/etc/apache2/sites-enabled/swhosting.com.conf
-
Edit the previous configuration file. In this example, we use the text editor nano, but you can use any other:
nano /etc/apache2/sites-enables/swhosting.com.conf
-
A first block corresponding to port 80 (http) appears:
ServerName swhosting.com
ServerAlias www.swhosting.com
DocumentRoot "/var/www/swhosting.com/datos/web"
```
- We add the following lines before closing "":
``` RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
```
```
-
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:
apache2ctl -t
It must appear Syntax OK. If an error appears, review the previous steps.
-
We reload the Apache configuration so that the changes are applied:
/etc/init.d/apache2 graceful
Then, access your domain with the prefix "http://" to see if it automatically redirects to "https://".
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 in which the Virtual Host configuration file is located and list the content. Habitually:
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:
listen 80;
root "/var/www/swhosting.com/datos/web";
index index.html index.php;
server_name swhosting.com www.swhosting.com;
``
-
We add the following line:
``
-
Being in the following way:
listen 80;
root "/var/www/swhosting.com/datos/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, appears something similar to:
ginx: configuration file /etc/nginx/nginx.conf test is successful
``
If an error appears, review the previous steps.
- We reload the Nginx configuration so that the changes are applied:
/etc/init.d/nginx reload
Then, access your domain with the prefix "http://" to see if it automatically redirects to "https://".