首页 > 解决方案 > 如何强制http到https

问题描述

我有一个移动应用程序,其 API 是用 laravel 构建的。url 是通过 http 请求完成的,但是服务器已更新为使用 https。我想在我的 htaccess 中强制我的请求从 http 到 https。我在这里检查了许多类似的问题,但我似乎没有改变线路来实现这一点,因为我看到了不同的解决方案,而且我的 htaccess 文件看起来与其他文件不完全相同。

这是我的文件:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

标签: phplaravel.htaccess

解决方案


尝试在 RewriteEngine On 之后添加以下脚本并尝试一下。

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

推荐阅读