LoginSignup
35
25

More than 5 years have passed since last update.

laravelでトレイリングスラッシュを有効にする方法

Last updated at Posted at 2015-03-28

Laravelはデフォルトだとトレイリングスラッシュ(URL末尾のスラッシュ)を消しちゃいます。

でもサービスによっては常にスラッシュを付与したい時ありますよね。
そんな時は.htaccessを下記のように編集しましょう。
※コメント解説付き

public/.htaccess(変更前。laravelデフォルト)
# Redirect Trailing Slashes...     【解説】URLに/があれば削除してリダイレクト ←ココが問題
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...    【解説】ファイルやディレクトリが存在しなければindex.phpへ
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

 ↓ トレイリングスラッシュを有効にしたい

public/.htaccess(変更後)
# ファイルやディレクトリが存在すれば表示 【解説】スラッシュ付与後だと判定できないので最初にチェック
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# add Trailing Slash       【解説】URLの末尾に/がなければ付与してリダイレクト ←これで解決
RewriteRule ^(.+[^/])$ /$1/ [R=301,L]

# Handle Front Controller...
RewriteRule ^ index.php [L]

これで常に末尾にスラッシュが付きます。
あとはよしなに。

35
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
25