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:
- Connect via FTP to the
data/webdirectory and locate the.htaccessfile. If it doesn’t exist, create it. - Add the following lines to the file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
- 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:
- 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 specified configuration file (example using
nano):
nano /etc/apache2/sites-enabled/swpanel.com.conf
- 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>
- Save the changes (
Ctrl + X, thenYandEnter). - Check the settings:
apache2ctl -t
The following should appear: Syntax OK.
- 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:
- Connect via SSH.
- Navigate to the Virtual Hosts directory:
cd /etc/nginx/sites-enabled
ls
- Edit the file for the domain (example using
nano):
nano /etc/nginx/sites-enabled/swpanel.com.conf
- 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;
}
- Save the changes (
Ctrl + X, thenYandEnter). - 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
- 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://.