Question

How to force https using .htaccess

Posted May 28, 2020 546 views
WordPress

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]
</IfModule>

…but I’m still having issues with browsers defaulting from my domain name (onestory.org) 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.

×
1 answer

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 http://example.com to load https://example.com. 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.

Regards,
Alex

  • 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 / https://onestory.org/

        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 / https://domain.com
          

          Regards,
          Alex

Submit an Answer