0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel × Docker】http:localhost/xxx が Not Foundになる原因と解決法

Last updated at Posted at 2025-05-30

はじめに

Laravel + Docker + Apache 環境で、ルーティングがうまく動かずNot Foundになる問題に直面。
原因と解決方法を備忘録として残します。

環境

開発環境 バージョン
windows 11
WSL Ubuntu 20.04
Laravel 12

🔥発生した問題

Laravelプロジェクトで以下のようなルートを定義しているのに。。。

routes/web.php
Route::get('/hogehoge', function() {
    return view('welcome');
});

ブラウザでアクセスすると、以下のようにエラーになります。

ブラウザ
http://localhost:8080/hogehoge
→ Not Found

しかし以下なら表示される。

ブラウザ
http://localhost:8080/index.php/hogehoge
→ 表示される(Laravelに届いている)

原因

Apacheが .htaccess を読み込んでいない(=URLをlaravelに渡せていない)。
・mod_rewrite モジュールが無効
・.htaccess の読み込みが無効(AllowOverride Noneになっている)

解決手順

① コンテナに入る

docker exec -it コンテナ名 bash

※コンテナ名は自分の環境に合わせてください。

② mod_writeを有効にする

a2enmod rewrite

③ Apacheの設定を修正する

vim /etc/apache2/apache2.conf

以下のように修正する

<Directory /var/www/html/public>
    AllowOverride None→これを「All」に変更する
    Require all granted
</Directory>

④ Apacheを安全に再起動

apache2ctl -k graceful

ブラウザで再度アクセスしてLaravelの画面が表示されれば成功!

ブラウザ
http://localhost:8080/hogehoge

なぜindex.php/xxx は動くのか?

Laravelのフロントコントローラは public/index.php にあるため、そこを直接指定すればルーティング処理が動きます。
ただし、本来は .htaccess により index.php を省略できるようにするのが正しい構成です。

最後に

最後まで閲覧いただきありがとうございました。
ご意見、ご指摘ありましたら、コメントいただけますと幸いです。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?