How to force https using .htaccess

Posted May 28, 2020 2.7k views

I’ve used the code from another answer to edit the .htaccess like so:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

…but I’m still having issues with browsers defaulting from my domain name ( to the raw IP address.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Hello, @ahaas

You can try with some of the following rules:

The following forces any http request to be rewritten using https. For example, the following code forces a request to to load It also forces directly linked resources (images, css, etc.) to use https:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

The below code when added to an .htaccess file will automatically redirect any traffic destined for http: to https:

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

Let me know how it goes.


  • I’ve tried codes both one at a time and then together. I’m still seeing some error messages on certain browsers (mobile, Chrome) that want to default to the IP (which of course is not verified by the SSL certificate).

    • I have a coworker who suggests creating a second virtual server just to handle http that listens on port 80 and forwards to https on port 443.

      • We logged into the console and updated Apache site config and put a redirect in the http site config virtual host.

        Redirect /

        edited by MattIPv4
        • Hello, @ahaas

          Thanks for getting back to me. This sounds like a solution, also keep in mind that you can use this exact syntax in the .htaccess file as well.

          Redirect 301 /



I have the same need as my colleague, I used the suggested code but there was no redirection, what can I have done wrong?