{"id":526,"date":"2019-11-14T06:30:23","date_gmt":"2019-11-14T06:30:23","guid":{"rendered":"http:\/\/blog.hostinger.io\/hostinger-tutorials\/%category\/sslforcing-https\/"},"modified":"2025-09-22T10:53:50","modified_gmt":"2025-09-22T10:53:50","slug":"force-https-using-htaccess","status":"publish","type":"post","link":"\/tutorials\/force-https-using-htaccess","title":{"rendered":"How to force HTTPS using .htaccess"},"content":{"rendered":"<p>After you buy an SSL certificate and install it on your website, it will be available over HTTP and HTTPS. However, it&rsquo;s better to use only the latter because it encrypts and secures your website&rsquo;s data. While Hostinger allows users to configure this setting in just one click, you can also use the <strong>.htaccess<\/strong> file to force HTTPS connection. This tutorial will show you how.<\/p><figure class=\"wp-block-image size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/How-to-Speed-Up-Your-Website-EN.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"2048\" height=\"566\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/public\" alt=\"\" class=\"wp-image-69260\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><p>\n\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-forcing-https-on-all-traffic\">Forcing HTTPS on All Traffic<\/h2><p>One of the many functions you can perform via <strong>.htaccess<\/strong> is the 301 redirect, which permanently redirects an old URL to a new one. You can activate the feature to force HTTPS on all incoming traffic by following these steps:<\/p><ol class=\"wp-block-list\">\n<li>Go to&nbsp;<strong>File Manager&nbsp;<\/strong>in your hosting panel and open <strong>.htaccess&nbsp;<\/strong>inside the&nbsp;<strong>public_html&nbsp;<\/strong>folder. If you can&rsquo;t locate it, make sure to <a href=\"\/tutorials\/locate-and-create-htaccess\">create or unhide it<\/a>.<\/li>\n\n\n\n<li>Scroll down to find <strong>RewriteEngine On<\/strong>&nbsp;and insert the following lines of code below it:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">RewriteEngine On \nRewriteCond %{HTTPS} off \nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]<\/pre>\n<\/li>\n\n\n\n<li><strong>Save<\/strong>&nbsp;the changes.<\/li>\n<\/ol><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Make sure that the line <strong>RewriteEngine On<\/strong> is not repeated twice. In case the line already exists, simply copy the rest of the code without it.<\/p><\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-forcing-https-on-a-specific-domain\">Forcing HTTPS on a Specific Domain<\/h2><p>Let&rsquo;s say that you have two domains: <strong>http:\/\/yourdomain1.com&nbsp;<\/strong>and&nbsp;<strong>http:\/\/yourdomain2.com. <\/strong>Both domains access the same website, but you only want the first one to be redirected to the HTTPS version. In this case, you need to use the following code:<code><\/code><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RewriteEngine On \nRewriteCond %{HTTP_HOST} ^yourdomain1.com [NC] \nRewriteCond %{HTTPS} off \nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<\/pre><p>Make sure to replace <strong>yourdomain1 <\/strong>with the actual domain you&rsquo;re trying to force HTTPS on.<\/p><h2 class=\"wp-block-heading\" id=\"h-forcing-https-on-a-specific-folder\">Forcing HTTPS on a Specific Folder<\/h2><p>The <strong>.htaccess&nbsp;<\/strong>file can also be used to force HTTPS on specific folders. However, the file should be placed in the folder that will have the HTTPS connection.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RewriteEngine On \nRewriteCond %{HTTPS} off \nRewriteRule ^(folder1|folder2|folder3) https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<\/pre><p>Make sure to change the <strong>folder<\/strong> references to the actual directory names.<\/p><p>After making the changes, clear your browser&rsquo;s cache and try to connect to your site via HTTP. If everything was added correctly, the browser will redirect you to the HTTPS version.<\/p><figure class=\"wp-block-image size-full\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-web-hosting\" href=\"\/web-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/public\" alt=\"Hostinger web hosting banner\" class=\"wp-image-98604\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Congratulations! you have successfully edited your <strong>.htaccess file<\/strong> and redirected all HTTP traffic to HTTPS, the safe version of your website. Depending on the platform where you developed your website, there could be alternative methods to enable this feature. For example, you can configure your WordPress or PrestaShop site to work with HTTPS using plugins.<\/p><p>If you have any tips, tricks, or suggestions that you want to share, we are looking forward to seeing them in the comments!<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Suggested Reading<\/h4>\n                    <p><a href=\"\/tutorials\/http-vs-https\">HTTP vs HTTPS: Comparison, Pros and Cons, and More<\/a><br>\n<a href=\"\/tutorials\/fix-mixed-content-wordpress\/\">WordPress Mixed Content Error: Learn How to Solve Incorrect SSL Settings<\/a><br>\n<a href=\"\/tutorials\/301-redirects-in-wordpress\">How to Set Up 301 Redirects in WordPress<\/a><\/p>\n                <\/div>\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After you buy an SSL certificate and install it on your website, it will be available over HTTP and HTTPS. [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/force-https-using-htaccess\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":25,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Force HTTPS using .htaccess (Updated %currentyear%)","rank_math_description":"Want to create a redirect from HTTP to HTTPS? See this step by step guide on how to force all visitors and traffic to HTTPS using htaccess.","rank_math_focus_keyword":"htaccess force https","footnotes":""},"categories":[22628,22630],"tags":[],"class_list":["post-526","post","type-post","status-publish","format-standard","hentry","category-hosting","category-technical-aspects"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/force-https-using-htaccess","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-forcar-acesso-ssl-em-meu-site","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/comment-forcer-le-https-a-laide-de-htaccess","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/como-forzar-https-usando-htaccess","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/force-https-htaccess","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/force-https-using-htaccess","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/ssl\/force-https-using-htaccess","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/ssl\/force-https-using-htaccess","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/como-forzar-https-usando-htaccess","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/como-forzar-https-usando-htaccess","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/como-forzar-https-usando-htaccess","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-forcar-acesso-ssl-em-meu-site","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/force-https-using-htaccess","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/force-https-using-htaccess","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/force-https-using-htaccess","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/force-https-using-htaccess","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=526"}],"version-history":[{"count":39,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":134778,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/526\/revisions\/134778"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}