Laravel .htaccess 重定向一体(1.public 2.https 3.www→非 www)

Laravel .htaccess redirect all in one (1.public 2.https 3.www→non-www)

  1. 我有 laravel 应用程序,其中 .htaccess 正在重定向到 Laravel 的 /public 文件夹。
  2. 今天我添加了 SSL 支持,并且我成功更新了 .htaccess 以将 http 重定向到 https
  3. 除此之外,我还需要它来将 www 重写为 non-www。有很多示例如何单独进行。但是我找不到如何结合前两个要求来做到这一点。 请帮忙
    我当前的 htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# for HTTPS
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#/ end of HTTPS

RewriteRule ^(.*)$ public/ [L]
</IfModule>
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Start with www 
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]


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


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

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

我更新了你现有的 .htaccess

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

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]

RewriteRule ^(.*)$ public/ [L]