LoginSignup
32
23

More than 5 years have passed since last update.

react-routerを使って静的コンテンツを配信する際のrewrite設定覚書

Last updated at Posted at 2016-12-12

react-router使ってアプリ作った際、ディレクトリ掘ってブックマークすると、
404 not foundになってハマるんですよね。

ということで、rewriteしないといけないんですけども、
調べた範囲でまとめました。

react-router サンプルコード

routerはbrowserHistoryを想定

src/index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';
import App from '../containers/App';

window.React = React;

ReactDOM.render((
  <Router history={browserHistory}>
    <Route path="/">
      <IndexRoute component={App} />
      <Route path="/:userName" component={App} />
    </Route>
  </Router>
), document.getElementById('root'));

httpd

public/.htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

Firebase Hosting

./firebase.json
{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

Netlify

公開ディレクトリに置く必要あり

public/_redirects
/* /index.html 200
32
23
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
32
23