0
0

Laravelでweb.phpにrouteを書いてもApacheの404エラーになるのは.htaccessの問題

Last updated at Posted at 2023-10-12

通常、web.phpにURIを書けば動くはずだが
404エラーになる。
それもLaravelの吐き出すグラフィカルな404ではなく
旧来のApacheが出力する文字だけの404
↑ここに気がつくのが遅くてずっとweb.phpやcacheを疑っていたが
Laravel10の.htaccessは

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

    RewriteEngine On

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

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

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

なんですが、
この
RewriteCond %{REQUEST_FILENAME} !-d
が曲者で
publicにディレクトリがなければって条件で
って意味なんですよね。

で、ワイは何をしていたかというと
publicに/event/enquete/imgというディレクトリを作り画像を置き
web.phpで
/event/enquete/login.htmlはLaravelを実行させたいと

Route::get('/event/enquete/login.html', ['uses' => 'App\Http\Controllers\EnquetemainviewController@index','as' => 'enquetemainview.index']);

していたら、
RewriteCond %{REQUEST_FILENAME} !-d
でpublicに/event/enquete/ディレクトリあるからhtaccessのindex.php行にはたどり着けずなので、Apacheに処理が返り、
でも、publicの/event/enquete/にはindex.htmlは無いから
Apacheが404エラーを出していた。。って寸法。

ということで、以下のように
publicに実ファイルが存在しなかったときのみ、Laravelのindex.phpが動くようにコメントアウトした。

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

    RewriteEngine On

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

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

    # Send Requests To Front Controller...
    #RewriteCond %{REQUEST_FILENAME} !-d  **コメントアウト
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

気づくのに時間がかかった・・悔しい

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