Laravel 使用 Mod 重写强制使用 HTTPS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17898207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:11:33  来源:igfitidea点击:

Laravel Forcing HTTPS with Mod Rewrite

apachemod-rewritesslhttpslaravel

提问by benedict_w

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

我有一个运行 Laravel 3 的站点,它需要在 apache 配置中使用以下重写规则来强制使用 https:

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.

这会正确强制 https 但所有 Laravel 路由都返回“未找到”(即未命中 index.php),如果我删除重写规则一切正常。

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

/public 文件夹中的 .htaccess 与 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/ [L]
</IfModule>

回答by Antonio Carlos Ribeiro

This .htaccess is working for me:

这个 .htaccess 对我有用:

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

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

回答by benedict_w

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

经过几个小时的调试,现在看起来很简单:我的默认 ssl 配置没有该行

AllowOverride All

To enable the htaccess to be read

启用 htaccess 读取