laravel – apache – 設定 .htaccess 將網址移除 index.php 與強制轉向 http to https

目前我在 laravel 10 已經內建 .htaccess 只需要修改為

<IfModule mod_rewrite.c>
    RewriteEngine On

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

    # 強制轉換 https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

    # 移轉 /index.php 到 /
    RewriteCond %{THE_REQUEST} \s/index\.php [NC]
    RewriteRule ^(?:[^/]+/)?index\.php(?:/(.*))?$ /$1 [L,R=301]

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

上方實測可以解決

  1. http:// 轉換到 https://
  2. /index.php 轉換到 /
  3. 當有 route 名稱的時候,例如 /index.php/about 強制轉換到 /about

發表迴響