在 Nginx 裡面,要設定 Laravel 的 URL Rewrite 很簡單:
location / { try_files $uri $uri/ /index.php?$query_string; }
這樣就搞定了。
在 Apache 2 的環境下要設定,倒也不難,可以直接到 Virtual host 檔案去設定,也可以直接在 web document root 底下設定 .htaccess 檔 (通常在外部租用虛擬主機就只能設定 .htaccess)
<IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] </IfModule>
另外,如果是租用虛擬主機,而 Web Server 是 Apache2 的時候,預設 web document root 常常會是在 public_html 底下,Laravel 4 可以直接更改 path.php 的內容去指定 Laravel 預設的 public 目錄,到了 Laravel 5 就沒有這個檔案可以設定,網路上有很多方法,譬如說加到 index.php 裡面去指定、server.php 去指定等等,我喜歡直接改預設的檔案。
/web_root/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
找一下 publicPath() 這個 function,把 public 改成 public_html 就可以了。
喔,如果是可以 SSH 連線的主機,那用 Symbolic Link 來設定也可以:
ln -s public_html puclic