vitozev vitozev - 2 months ago 19
Apache Configuration Question

Forcing http to https append "public" in URL

I have the following

.htaccess
inside
webroot
directory:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/admin/)
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>


Then, inside
public
directory I have the following
.htaccess


<IfModule mod_rewrite.c>
RewriteEngine On


# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]

# Force HTTPS
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


</IfModule>


Then, if I hit the following URL:

http://example.com/

The browser is redirected to:

https://example.com/public/

When site is loaded over HTTP (without forcing), this part is omitted.

Answer

Force https rule must be placed in root .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On

   # Force HTTPS
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

    RewriteRule ^$ public/     [L]

    RewriteCond %{REQUEST_URI} !^(/admin/)
    RewriteRule (.*) public/$1 [L]
</IfModule>

Remove http->https rule from public/.htaccess and clear browser cahce to test this.

Comments