Automatically redirect to secure connection 'https'
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:
- Connect via FTP to the
datos/webdirectory and locate the.htaccessfile. If it does not exist, create it. - Add the following lines to the file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
- Save the changes.
Notice: Some CMS, such as WordPress, modify
.htaccessautomatically. 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:
- Connect via SSH.
- Display the Virtual Hosts configuration with:
apache2ctl -S - 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
- Edit the indicated configuration file (example with
nano):
nano /etc/apache2/sites-enabled/swpanel.com.conf
- 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>
- Save the changes (
Ctrl + X, thenYandEnter). - Check the configuration:
apache2ctl -t
It should appear: Syntax OK.
- 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:
- Connect via SSH.
- Access the Virtual Hosts directory:
cd /etc/nginx/sites-enabled
ls
- Edit the file corresponding to the domain (example with
nano):
nano /etc/nginx/sites-enabled/swpanel.com.conf
- 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;
}
- Save the changes (
Ctrl + X, thenYandEnter). - 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
- Reload Nginx to apply the changes:
/etc/init.d/nginx reload
Access your domain with http:// to verify that it automatically redirects to https://.