Recently the project we've been working on is setup to use HTTPS instead of normal HTTP request. In this blog post we'll see how can we redirect all our HTTP Request to HTTPS URL.

Note*: To achieve this we'll be writing a small code snippet in .htaccess file. Generally .htaccess file is located at your site's root directory.

Code snippet:

<IfModule mod_rewrite.c>
  RewriteEngine on

  # Redirect HTTP to HTTPS
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

Add above code in .htaccess file, below RewriteEngine on.

Note*:If you're working on Windows OS with IIS, you'll need to add code into web.config file instead of .htaccess. Check this documentation for redirection code in web.config file.

Reference: