Yogesh Chauhan's Blog

Forcing the domain to serve securely using HTTPS

in Miscellaneous on January 10, 2021

If you have added an SSL certificate to your domain, you can force all visits to your site to use HTTPS to ensure your traffic is secure.

You need to create/edit an .htaccess file on your server.

Where should the .htaccess file be created/located?

The .htaccess file should be located in your website’s primary directory.

If your site is ‘example.com’, the .htaccess file would go in the /home/username/example.com directory. You can also confirm your site’s web directory in the panel.

The following forces any http request to be rewritten using https.

For example, the following code forces a request to http://example.com to load https://example.com.

It also forces directly linked resources (images, css, etc.) to use https:


RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

If this isn’t working for you, first check your line endings.

Copy/paste from your web browser into a text editor may not work right, so after pasting into your text editor you should delete each line break and add it back in (line break = return key).


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Colleague UI Basics: The Search AreaColleagueHow to add onclick event to html elements dynamically using JavaScript?JavaScriptUnderstanding the confusing and handy “This” keyword in JavaScriptJavaScriptThe ALPHA function in Envision BasicEnvision BasicOverview of Drupal and how it worksDrupalHow to apply style only to first child and/or only to children other than the first child?CSSWhat is PostgreSQL? How similar or different it is from SQL?PostgresHow to create a simple stopwatch using JavaScript?JavaScriptthe box-sizing property in CSSCSSHow to remove special characters (dash, asterisk etc) from any string in PHP?PHPOptional arguments, Default parameters and REST parameters in JavaScriptJavaScript2 Ways We Can Write Multiple Line Commands in PHPPHP