Laravel Forcing HTTPS with Mod Rewrite

2019-06-03 03:07发布

问题:

I have a site running Laravel 3 which needs to force https using the following rewrite rule in apache config:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This forces https correctly but all the Laravel routes return 'Not Found' (i.e. not hitting index.php), if I remove the rewrite rule everything works.

The .htaccess inside the /public folder is as normal for Laravel:

<IfModule mod_rewrite.c>
     Options +FollowSymLinks
     RewriteEngine On
</IfModule>

# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

回答1:

This .htaccess is working for me:

<IfModule mod_rewrite.c>
    #Options -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://mysite.com/$1 [R,L]
</IfModule>


回答2:

After some hours debugging it seems simple now: my default-ssl config did not have the line

AllowOverride All

To enable the htaccess to be read