Liglo App
Liglo App

Reputation: 3819

.htaccess: Combine CakePHP rules with HTTPS redirect

My current .htaccess for working with CakePHP 3 is:

./

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]    
</IfModule>

./webroot

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

My host provider recommends to set up the following .htaccess for automatic https redirects:

RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://ihredomain.de/$1 [R=301,L]

How am I supposed to combine all these into one working htaccess without breaking Cake's routing?

I am somewhat afraid of the trial-and-error way, because it is a production system.

Upvotes: 1

Views: 704

Answers (2)

Ralph Segi
Ralph Segi

Reputation: 591

In the root folder i changed the .htaccess file to

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase    /
    RewriteCond    %{SERVER_PORT} 80
    RewriteRule    ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
    RewriteRule    ^(\.well-known/.*)$ $1 [L]
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

That worked for me while all other solutions didn't.

Upvotes: 1

Amit Verma
Amit Verma

Reputation: 41219

You can use this in your /.htaccess :

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://ihredomain.de/$1 [R=301,L]
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]    
</IfModule>

Upvotes: 0

Related Questions