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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
WordPress: How to print ACF repeater field values?WordPressWhat’s new in Constructor in PHP 8?PHPExplanation of PostgreSQL PgAdmin interfacePostgresHow JavaScript classes allows us to do less typing (syntactic sugar)?JavaScriptWhat’s the difference between visibility: hidden and display: none?CSSCrypto.getRandomValues() method in JavaScriptJavaScriptGap in Flex?CSSSolution to “TypeError: ‘x’ is not iterable” in Angular 9AngularHow to use PHPMailer to send an Email via Gmail SMTP Server?PHPJavaScript Number MethodsJavaScriptLIMIT and OFFSET in PostgresPostgresHAVING Clause in Postgres with Examples in All Aggregate FunctionsPostgres