首页 > 解决方案 > 带有强制 HTTPS 的 Laravel .htaccess

问题描述

我在我的 Laravel 网站的根目录中使用这个 .htaccess 文件进行托管,因此访问者在 URL 地址中看不到 /public/:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^my-domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.my-domain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

但到目前为止,我找不到添加强制 HTTPS 重定向的简单解决方案。请问有什么建议吗?

标签: laravelapache.htaccesssslhttps

解决方案


使用以下代码修改您的 .htaccess:

<IfModule mod_rewrite.c>
Options -Indexes 
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
</IfModule>

推荐阅读