How to Redirect HTTP to HTTPS in Nginx & Apache?

In order to change the URL of the page displayed in the search results, Google recommends using 301 Redirect. This is the best way to make sure that search engines and users will be directed to the correct page, and also protect themselves from losing traffic from search engines.

    • 1

      In what cases is it worth using a 301 redirect?

      • If there are several identical versions on different subdomains or folders, for example, http://domain.site.tld or http://site.tld or httpS://domain.site.tld, to redirect the visitor to the main version of the site.
      • Due to rebranding (or for other reasons), when you want users to not notice the difference when they click on the same URL. When gluing (combining) two sites so that links from all pages of the first redirect to a new one.
    • 2

      How to configure 301 Redirect from HTTP to HTTPS in Apache

      Normally, there are at least two virtual hosts in use when you are using an SSL certificate. Ordinary server requests should be served by port 80, and SSL requests served by port 443. Use ordinary redirect directive in you wish to redirect users from non-SSL to SSL site. In order to enable redirection using Apache, you must enable mod_rewrite:

      LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
                                      

      For the changes to take effect, you reboot the Apache web server:

      apache2ctl restart
                                      

      In .htaccess to redirect from HTTP to HTTPS add:

      <VirtualHost *:80>
          RewriteEngine On
          RewriteCond %{SERVER_PORT} !^443$
          RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
      </VirtualHost>

      Save and close the file, then restart the HTTP sever using next command:

      $ sudo systemctl restart apache2 [Ubuntu/Debian]
      $ sudo systemctl restart httpd [RHEL/CentOS]
                                      

      One page redirect, instead entire website

      <VirtualHost *:80>
          RewriteEngine On
          RewriteRule ^apache-redirect-http-to-https.html$ https://www.domain.tld/apache-redirect-http-to-https.html [R=301,L]
      </VirtualHost>

      Hints about .htaaccess redirects

      • Do not duplicate RewriteEngine On;
      • The RewriteCond and RewriteRule must immediately follow the RewriteEngin on/
    • 3

      NGNIX redirect from HTTP to HTTPS

      Use this tutorial to redirect all your visitors to HTTPS with WWW domain prefix using. That method uses 301 redirect technics and is safe for SEO according to Google best practices. You will need to edit the Nginx virtual host configuration file to forcefully redirect all users to HTTPS://www.domain.tld using the described example below.

      # Force all users to https://www.domain.tld
      
      server {
          listen 80;
          server_name domain.tld www.domain.tld;
          return 301 https://www.domain.tld$request_uri;
      }
      server {
          listen 443 ssl;
          server_name domain.tld;
      
          ssl_certificate /etc/nginx/ssl/www.domain.tld.pem;
          ssl_certificate_key /etc/nginx/ssl/www.domain.tld.key;
          return 301 https://www.domain.tld$request_uri;
      }
      
      server {
          listen 443 ssl;
          server_name www.domain.tld;
          root /var/www/html
      
          error_page 403 /error/404.html;
          error_page 404 /error/404.html;
          error_page 500 502 503 504 /error/50x.html;
      
          ssl_certificate /etc/nginx/ssl/www.domain.tld.pem;
          ssl_certificate_key /etc/nginx/ssl/www.domain.tld.key;
      }
      

      Restart the Nginx web server to put the changes into effect using the command:

      sudo systemctl restart Nginx                          
    • Conclusion

      Today it is mandatory to use the HTTPS protocol to protect all the traffic between your website visitors and the server. We suggest Nginx web server as the most powerful and simplest way to set up your website with permanent 301 redirects.

  • SSL
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

DigiCert and Sectigo SSL Installation manuals

Our team prepared the list of the most common resources to help you Generating CSR Code,...