Switching your website to HTTPS is an important step for security and user trust. If your Linux-based web server still allows access via plain HTTP, you are putting your visitors and data at risk. In this guide, we’ll explain how to redirect all traffic from HTTP to HTTPS using common Linux server configurations.
Switching to HTTPS (SSL/TLS) ensures:
Once you have installed an SSL certificate, the next step is to route all traffic via the secure HTTPS protocol.
If your server uses Apache, you can configure the redirection as follows.
Make sure that
is activated:mod_rewrite
sudo a2enmod rewrite
sudo systemctl neustart apache2
Open your HTTP
) Configuration file for the virtual host:(
port 80
sudo nano /etc/apache2/sites-available/000-default.conf
Then insert this within the
: block
RewriteEngine Ein
RewriteCond %{HTTPS} aus
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Save and exit the file and then restart Apache:
sudo systemctl restart apache2
If your server uses Nginx, the redirection is performed in the server block of the website.
sudo nano /etc/nginx/sites-available/default
Add this block
above your existing
for HTTPS:server
block
server {
listen 80;
server_name ihredomain.de www.yourdomain.com;
return 301 https://$host$request_uri;
}
Replace
with your actual domain name. Then test and reload:yourdomain.com
sudo nginx -t
sudo systemctl reload nginx
You can also combine HTTPS enforcement with the canonicalization of domains.
Example: Redirection from www to non-www (with HTTPS) in Nginx:
server {
listen 80;
server_name www.yourdomain.com;
return 301 https://yourdomain.com$request_uri;
}
Once you have made the changes, test them:
http://yourdomain.com
– it should redirect to https://yourdomain.com
.