skip to Main Content

I am trying to force https with my .htaccess file but when I do, the layout breaks and I get 404 errors in console for pretty much everything from css files to js files etc. I am using a MVC structure.

In my config file I have:

define('URLROOT', 'https://www.example.com');

In my header all my css files are called like this:

<link href="<?php echo URLROOT; ?>/css/style.css" rel="stylesheet">

My .htaccess looks like:

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteRule ^$ public/ [L]
 RewriteRule (.*) public/$1 [L]
 RewriteCond %{HTTPS} off
 RewriteRule (.*) https://www.example.com/$1 [R=301,L]
</IfModule>

Also, If I type in https://example.com it goes to https but if I just type in example.com it defaults to http which is also a problem.

2

Answers


  1. Move your http -> https rule on top:

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

    Make sure to test in a new browser to avoid old browser cache.

    Login or Signup to reply.
  2. Place this on top of your .htaccess, make sure to change the domain name and you are set – all HTTP requests (even those to static files) will get redirected to HTTPS:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule .* https://www.example.com/%{REQUEST_URI} [R,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search