.htaccess Laravel HTTP到HTTPS重定向无法工作

4zcjmb1e  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(148)

尝试了这么多的解决方案,但没有工作;下面是我.htaccess文件。
HTTPS网站工作没有问题,例如,当我点击菜单或徽标图标时,它会重定向到https://abc123.com,但当我从https中删除s并键入www.example.com时HTTP://abc123.com,它不会重定向。
我在PHP 7.4环境中使用过Laravel 8.8,最近也迁移到了PHP 8..但似乎什么都不起作用,
已经试过了

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        URL::forceScheme('https');
    }
}

但这只会导致500个错误。
我错过了什么?

RewriteEngine on
RewriteCond %{HTTP_HOST} abc123.com\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://abc123.com/$1 [R,L]
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

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

# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{HTTP_HOST} ^abc123\.com [NC]
# RewriteCond %{SERVER_PORT} 80
# RewriteRule ^(.*)$ https://abc123.com/$1 [R,L]

# Header always set Content-Security-Policy: upgrade-insecure-requests
# RewriteEngine On
# RewriteCond %{HTTPS} !=on
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# BEGIN Expire headers  
<IfModule mod_expires.c>  
  # Turn on the module.
  ExpiresActive on
  # Set the default expiry times.
  ExpiresDefault "access plus 2 days"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/svg+xml "access 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
  ExpiresByType image/ico "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"
  ExpiresByType text/html "access plus 600 seconds"
</IfModule>  
# END Expire headers
# RewriteEngine On
# RewriteCond %{HTTP_HOST} abc123\.com [NC]
# RewriteCond %{SERVER_PORT} 80
# RewriteRule ^(.*)$ https://abc123.com/$1 [R,L]


#RewriteEngine On
#RewriteCond %{HTTP_HOST} abc123.com\.com [NC]
#RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://abc123.com/$1 [R,L]

<IfModule mime_module>
  AddHandler application/x-httpd-ea-php80 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

已经评论了我到目前为止所尝试的,,也许我错过了一些东西。

o75abkj4

o75abkj41#

我已经把问题整理好了......为了以后参考,我把答案贴在这里。

RewriteEngine on
RewriteCond %{HTTPS} !=on   
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这就解决了我的问题。

相关问题