2

I am fairly new to React and am trying to utilize the react router feature. I am displaying Static pages on a TV screen pointing to a certain URL.

When in my Dev environment everything is working fine. When I enter the correct path it points to the set component. When loading it onto my hosting server and testing on a Live URL pointing to the desired path results in a 404.

import React, { Component } from 'react';
import './style/App.css';
import Header from './js/components/header';
import Menu from './js/components/menu';
import MenuTwo from './js/components/menuTwo';
import {  BrowserRouter as Router,  Route, Switch, Link } from 'react-
router-dom';

class App extends Component {
  render() {
   return (
     <Router>
  <div>
      <Header headerProp="maxwell's pizzeria" /> 
      <Route path="/" exact component={Menu} />
      <Route path="/menuTwo" exact component={MenuTwo} />

  </div>

  </Router>
  )
 }
}

export default App;

The webpack.config which I am also not very familiar with is as follows:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'public');
var APP_DIR = path.resolve(__dirname, 'src/build');

// Existing Code ....
var config = {
 // Existing Code ....
 module : {
  loaders : [
   {
    test : /\.jsx?/,
    include : APP_DIR,
    loader : 'babel'
  }
  ]
 }
}

module.exports = config;
2
  • is the path on the remote server the same as local test server? e.g. myserver.com/ vs localhost:8080/ or myserver.com/path vs localhost:8080/path, maybe you deployed your app to the wrong location: "You can control this with the homepage field in your package.json" Commented Feb 23, 2018 at 0:35
  • Your webserver is probably not configured correctly. It needs to serve your app on every route requested as your app takes care of the routing below / itself. If you request e.g. /menuTwo it will probably try to serve the page located locally on your server which is not existing. It needs to serve your app on every route, not only on /. Commented Feb 23, 2018 at 1:01

1 Answer 1

3

First check your homepage entry in the package.json.

"homepage": "http://example.com/path/to/app",

then you may try some rewrite rule to point to your apps in the.htaccess file:

#Options All -Indexes
Options -Indexes
<Files *.php>
    ForceType application/x-httpd-php5
</Files>
# BEGIN My App Web-App
<ifModule mod_rewrite.c>
  Options +FollowSymLinks
  IndexIgnore */*
  RewriteEngine On
  RewriteBase /path/to/app/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*) /path/to/app/index.html
</ifModule>
# END   My App Web-App

EDIT

And of course set the basename on the Router

<Router basename='/path/to/app'>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.