How do I set the website to always use HTTPS (SSL)?
Add the code below into the .htaccess file in the root directory of your website:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
1. Case Study
You have created a website and then set up SSL for your website, with the hope that visitors who open your website are always served with the HTTPS protocol .
But when you try to type in the domain in the browser, for example yourwebsite.com, the open website still uses the http://websiteanda.com protocol, the padlock icon is still open or red, which means that communication between visitors and the server is still not secure, haven't you? not encrypted properly.
Also read: How to Install Free SSL with Let's Encrypt
2. How to Add
- You can use the cPanel/Direct Admin/ hosting panel that you have, then open the File Manager application to edit the .htaccess file , or
- Create/Edit an .htaccess file in notepad, then transfer the file using FTP/SFTP/SCP with additional applications such as FileZilla and WinSCP .
3. Code Explanation
RewriteEngine On
Activate the rewrite module , to change the link according to our settings.
RewriteCond %{HTTPS} off
Conditioning the rewrite ( rewrite ) according to certain conditions, in this case if the website is opened with HTTP:// (or if HTTPS is off).
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The rewrite rule, in this case the HTTP link is changed to HTTPS, and is permanent (301).
4. Additional Notes
- Adjust the .htaccess file with the folder/directory where your website is located.
- If your website is in a subfolder/subdirectory, then the .htaccess that must be changed is the one in that subfolder/subdirectory.
5. Cover
That 's how to set a website to always use HTTPS (SSL) when it is opened by visitors who come.
By using HTTPS, visitor data can be more secure, improve user experience and an additional step for search engine ranking optimization Google / Bing (SEO).
Hopefully useful, Happy Digitalyze your life!