LoginSignup
1
1

More than 3 years have passed since last update.

React RouterのBrowserRouterを使うと404が返ってくる

Posted at

React RouterのBrowserRouterを使っている場合、ブラウザバックしたり下層ページのURLを直打ちしてアクセスすると404が返ってくる。
これはリクエストしているリソースへのアクセスをサーバが拒否しているからだが、ワイルドカードでルーティングをしてやれば解決する。

例えばRuby on Railsなら以下のようにルーティングを書いておく。

config/routes.rb

Rails.application.routes.draw do
  match '*path', to: 'react#index', via: :get
end

app/controllers/react_controller.rb

class ReactController < ApplicationController
  def index
  end
end

このとき app/views/react/index.erb 等のviewファイルは必要ない。

参考

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