Laravel 在 Apache 中如何部署 .htaccess

方法1

根目錄的中的.htaccess設定為

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 確保請求不是已經指向實際文件或目錄
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    # 移除尾部斜線
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # 主要重定向規則
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/$1 [QSA,L]

</IfModule>

我在 LiteSpeed 主機中也是用這 .htaccess

至於public/.htaccess 則不要做任何更改

方法2

如果你可以修改Custom HTTPD文件, 類似openlitespeed.conf文件,那麼修改為預設目錄是public 下面這個是更佳的 .htaccess,把domain/public 也防止了錯報

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

    RewriteEngine On

    # 移除查詢字串中的斜線
    RewriteCond %{THE_REQUEST} \s/+([^?]*)/(?:\?|\s) [NC]
    RewriteRule ^ /%1 [R=301,L,QSA]

    # 如果請求已經指向 public,直接跳過
    RewriteCond %{REQUEST_URI} ^/public/ [NC]
    RewriteRule ^ - [L]

    # 所有其他請求內部重寫到 public 目錄
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ public/$1 [L,QSA]

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

後來發現其實在apache還有一個問題,也不知算不算是apache問題,準確說應該是Virtual hosting 虛擬主機/網頁寄存的問題

因為Laravel 的filesystem... 內置放了在 app_root/storage/,要用Linux 指令生成Symbolic link,不是每間hosting 都會給你有輸入Linux指令的權限

我相信有可能要去了解一下 config/filesystems.php 有沒有更改storage_path() 目錄的可能性,