{"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":"2026-11-05T09:38:50","modified_gmt":"2026-11-05T09:38: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":"\n<p class=\"wp-block-paragraph\">To force HTTPS using <code>.htaccess<\/code>, add a 301 redirect that sends HTTP requests to the HTTPS version of the same URL. You can apply the redirect to your entire website, a specific domain, or a specific folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before adding the rule, make sure your SSL certificate is installed and the HTTPS version of the site loads correctly. This tutorial also covers how to verify the redirect, fix common redirect loops, and handle proxy or CDN setups where <code>%{HTTPS}<\/code> may not detect the original visitor protocol correctly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 id=\"h-which-https-redirect-should-you-use\" class=\"wp-block-heading h-t-title-2\">Which HTTPS redirect should you use?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Choose the HTTPS redirect based on how much of your website should use HTTPS.<\/p>\n\n\n\n<figure tabindex=\"0\" class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Redirect scope<\/th><th>Use it when<\/th><\/tr><tr><td>All traffic<\/td><td>You want every HTTP request on the website to redirect to HTTPS. This is the best choice for most websites.<\/td><\/tr><tr><td>Specific domain<\/td><td>Multiple domains point to the same website, but you only want to force HTTPS for one domain.<\/td><\/tr><tr><td>Specific folder<\/td><td>You only want requests within a particular directory to use HTTPS.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Before adding any redirect, make sure the HTTPS version of your website already loads correctly with a valid SSL certificate. An <code>.htaccess<\/code> rule redirects visitors to HTTPS, but it does not install or configure the SSL certificate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your website is hosted with Hostinger and you only need to redirect the entire site, you can also enable the built-in HTTPS redirect instead of editing <code>.htaccess<\/code> manually.<\/p>\n\n\n\n<h2 id=\"h-how-to-force-https-on-all-traffic\" class=\"wp-block-heading h-t-title-2\">How to force HTTPS on all traffic<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Force HTTPS on all traffic when you want every HTTP request on the website to redirect permanently to its HTTPS equivalent. This is the best option for most websites using <code>.htaccess<\/code>.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Open <strong>File Manager<\/strong> in your hosting control panel and locate the <code>.htaccess<\/code> file in your website\u2019s document root, such as <code>public_html<\/code>. If the file is missing, <a data-click-id=\"hgr-post-content-link-create-or-unhide-the\" href=\"\/tutorials\/locate-and-create-htaccess\/\">create or unhide the<\/a> <code><a data-click-id=\"hgr-post-content-link-htaccess\" href=\"https:\/\/www.hostinger.com\/tutorials\/locate-and-create-htaccess\/\">.htaccess<\/a><\/code> <a data-click-id=\"hgr-post-content-link-file\" href=\"https:\/\/www.hostinger.com\/tutorials\/locate-and-create-htaccess\/\">file<\/a>.<\/li>\n\n\n\n<li>Back up the existing <code>.htaccess<\/code> file before making changes.<\/li>\n\n\n\n<li>Add the following rule:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>Save the file.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The rule works as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>RewriteEngine On<\/code> enables Apache\u2019s rewrite engine.<\/li>\n\n\n\n<li><code>RewriteCond %{HTTPS} off<\/code> applies the following rule only when the current request is using HTTP rather than HTTPS.<\/li>\n\n\n\n<li><code>RewriteRule<\/code> redirects the request to the same host and path using <code>https:\/\/<\/code>.<\/li>\n\n\n\n<li><code>R=301<\/code> sends a permanent redirect response.<\/li>\n\n\n\n<li><code>L<\/code> tells Apache to stop processing further rewrite rules for that request once this rule matches.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>RewriteEngine On<\/code> already exists in the <code>.htaccess<\/code> file, don\u2019t add a second copy.<\/p>\n\n\n\n<h2 id=\"h-how-to-force-https-on-a-specific-domain\" class=\"wp-block-heading h-t-title-2\">How to force HTTPS on a specific domain<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Force HTTPS on a specific domain when multiple domains point to the same website but only one should redirect from HTTP to HTTPS.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if <code>yourdomain1.com<\/code> and <code>yourdomain2.com<\/code> use the same document root but you only want to force HTTPS for <code>yourdomain1.com<\/code>, add:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RewriteEngine On\nRewriteCond %{HTTP_HOST} ^(www\\.)?yourdomain1\\.com$ [NC]\nRewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>yourdomain1.com<\/code> with the domain you want to redirect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first condition limits the rule to that hostname, including its <code>www<\/code> version. The second condition makes sure the redirect only runs for HTTP requests. Other domains using the same <code>.htaccess<\/code> file aren\u2019t affected by this rule.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>RewriteEngine On<\/code> already exists in your <code>.htaccess<\/code> file, don\u2019t add it again.<\/p>\n\n\n\n<h2 id=\"h-how-to-force-https-on-a-specific-folder\" class=\"wp-block-heading h-t-title-2\">How to force HTTPS on a specific folder<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Force HTTPS on a specific folder when only one directory and its contents should redirect from HTTP to HTTPS.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Open the folder you want to protect in File Manager.<\/li>\n\n\n\n<li>Open or create the <code>.htaccess<\/code> file inside that folder.<\/li>\n\n\n\n<li>Add the following rule:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule ^ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>Save the file.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Because the <code>.htaccess<\/code> file is inside the target folder, the rule automatically applies to requests for that directory and its subdirectories. You don\u2019t need to include the folder name in <code>RewriteRule<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, placing this <code>.htaccess<\/code> file inside <code>\/members\/<\/code> redirects requests under <code>http:\/\/example.com\/members\/<\/code> to HTTPS without applying the rule to unrelated directories.<\/p>\n\n\n\n<h2 id=\"h-how-to-verify-the-https-redirect\" class=\"wp-block-heading h-t-title-2\">How to verify the HTTPS redirect<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Verify the redirect by opening an HTTP version of a URL covered by your <code>.htaccess<\/code> rule and confirming that the browser automatically loads the HTTPS version.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Open a private or incognito browser window to avoid a previously cached redirect.<\/li>\n\n\n\n<li>Enter an <code>http:\/\/<\/code> URL covered by the rule. For example, use your homepage for an all-traffic redirect or a URL inside the target directory for a folder-specific redirect.<\/li>\n\n\n\n<li>Confirm that the browser changes the URL to <code>https:\/\/<\/code> and loads the page without a certificate warning.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">You can also check the HTTP response from the command line:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -I http:\/\/example.com\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A working permanent redirect should return a <code>301<\/code> response with a <code>Location<\/code> header pointing to the HTTPS URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the HTTP URL doesn\u2019t redirect, redirects repeatedly, or the HTTPS page shows a certificate error, troubleshoot the configuration before relying on the rule.<\/p>\n\n\n\n<h2 id=\"h-how-to-troubleshoot-https-redirect-problems\" class=\"wp-block-heading h-t-title-2\">How to troubleshoot HTTPS redirect problems<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">HTTPS redirect problems usually come from conflicting redirects, an SSL configuration issue, or a proxy\/CDN setup where Apache can\u2019t detect the visitor\u2019s original protocol correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\">Fix an HTTPS redirect loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the browser shows <strong>ERR_TOO_MANY_REDIRECTS<\/strong>, temporarily remove or disable the new <code>.htaccess<\/code> rule and test the website again.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then check whether HTTPS is already being forced somewhere else, such as your hosting control panel, CMS, plugin, or CDN. Running multiple redirect methods at the same time can create conflicting rules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For Hostinger websites, follow the <a data-click-id=\"hgr-post-content-link-ssl-redirect-loop-troubleshooting-steps\" href=\"https:\/\/www.hostinger.com\/support\/2197891-how-to-fix-ssl-redirect-loop-at-hostinger\/\">SSL redirect loop troubleshooting steps<\/a> to check duplicate redirects, Cloudflare settings, and WordPress URLs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\">Fix redirect loops behind a proxy or CDN<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A reverse proxy, load balancer, or CDN can terminate the HTTPS connection before forwarding the request to your web server. In that setup, Apache may see the connection as HTTP, so <code>%{HTTPS}<\/code> remains <code>off<\/code> even when the visitor opened an HTTPS URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your proxy reliably sets and overwrites the <code>X-Forwarded-Proto<\/code> header, use that header instead of <code>%{HTTPS}<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RewriteEngine On\nRewriteCond %{HTTP:X-Forwarded-Proto} =http [NC]\nRewriteRule ^ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Only use this rule when your hosting provider or proxy documentation confirms that <code>X-Forwarded-Proto<\/code> is set by a trusted upstream proxy. Otherwise, the header can be spoofed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your CDN already provides an HTTPS redirect feature, use one redirect layer instead of forcing HTTPS both at the CDN and origin server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\">Fix a redirect that doesn\u2019t work<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If an HTTP URL still loads without redirecting, check that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the <code>.htaccess<\/code> file is in the correct directory<\/li>\n\n\n\n<li><code>mod_rewrite<\/code> is available and <code>.htaccess<\/code> overrides are enabled<\/li>\n\n\n\n<li>the rule was saved correctly<\/li>\n\n\n\n<li>another rewrite rule isn\u2019t taking precedence<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If the HTTPS URL itself shows a certificate warning or SSL error, fix the SSL certificate first. An <code>.htaccess<\/code> redirect only sends visitors to HTTPS; it doesn\u2019t repair an invalid or missing certificate.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a data-click-id=\"hgr-post-content-link-web-hosting\" class=\"hgr-tutorials-cta hgr-tutorials-cta-web-hosting\" href=\"\/web-hosting\" target=\"_blank\" rel=\"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>\n\n\n\n<h2 id=\"h-what-should-you-do-after-forcing-https\" class=\"wp-block-heading h-t-title-2\">What should you do after forcing HTTPS?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After confirming the redirect works, check that your website also loads its images, scripts, stylesheets, and other resources over HTTPS. A page can redirect correctly and still show a <strong>Not secure<\/strong> warning if some resources use <code>http:\/\/<\/code> URLs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If that happens, fix the insecure resource URLs rather than adding another redirect rule. WordPress users can follow our guide to <a data-click-id=\"hgr-post-content-link-fixing-mixed-content-errors-in-wordpress\" href=\"\/tutorials\/fix-mixed-content-wordpress\/\">fixing mixed content errors in WordPress<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your site uses Hostinger Web or Cloud hosting, HTTPS is normally forced automatically after SSL installation, so manual <code>.htaccess<\/code> redirects are mainly useful when you need custom redirect behavior.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To force HTTPS using .htaccess, add a 301 redirect that sends HTTP requests to the HTTPS version of the same [&#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","rank_math_description":"Learn how to force HTTPS using .htaccess for all traffic, a specific domain, or a folder, then verify the redirect and fix common issues.","rank_math_focus_keyword":"htaccess force https","_content_updated_date":"2019-11-14","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":"it-IT","link":"https:\/\/www.hostinger.com\/it\/tutorial\/come-forzare-https-con-htaccess\/","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/force-https-using-htaccess","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/force-https-using-htaccess","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/force-https-using-htaccess","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},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/como-forzar-https-usando-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":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-forcar-acesso-ssl-em-meu-site\/","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":40,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":161330,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/526\/revisions\/161330"}],"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}]}}