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.

Why redirect to HTTPS?

Switching to HTTPS (SSL/TLS) ensures:

  • Encrypted communication between users and your server
  • Improved SEO ranking (Google prefers HTTPS)
  • Trust indicators such as the padlock symbol in the browser
  • Compliance with modern web standards

Once you have installed an SSL certificate, the next step is to route all traffic via the secure HTTPS protocol.

Apache: Redirect HTTP to HTTPS

If your server uses Apache, you can configure the redirection as follows.

Step 1: Activate the rewrite module

Make sure that mod_rewrite is activated:

sudo a2enmod rewrite
sudo systemctl neustart apache2

Step 2: Update the configuration of your virtual host

Open your HTTP(port 80) Configuration file for the virtual host:

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

Nginx: Redirect HTTP to HTTPS

If your server uses Nginx, the redirection is performed in the server block of the website.

Step 1: Edit the Nginx configuration file

sudo nano /etc/nginx/sites-available/default

Step 2: Add a redirect block

Add this block above your existing server block for HTTPS:

server {
    listen 80;
    server_name ihredomain.de www.yourdomain.com;

    return 301 https://$host$request_uri;
}

Replace yourdomain.com with your actual domain name. Then test and reload:

sudo nginx -t
sudo systemctl reload nginx

Optional: Redirect from www to non-www (or vice versa)

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;
}

Testing the redirection

Once you have made the changes, test them:

Final notes

  • Make sure your SSL certificate is valid and installed before forcing HTTPS.
  • Always use 301 (permanent) redirects to avoid SEO issues.
  • Use redirect logic at web server level, not via PHP or JavaScript.