【问题标题】:How to fix page refresh issues in reactjs如何修复 reactjs 中的页面刷新问题
【发布时间】:2019-03-24 16:45:24
【问题描述】:

当我按 F5/刷新网页时,我在 apache 2.2 托管的 react 应用程序上收到 404 not found 错误

我正在使用 react 引导带和路由运行 reactjs

我看到网上有人在公共文件夹中添加了一个.htaccess 文件,然后构建了应用程序,但这对我不起作用

import './App.css';
import { BrowserRouter as Router,
        Route,
        Link,
        Switch,
        Redirect
 } from 'react-router-dom';
import Home from './components/Home';
import Test from './components/Test';
import Test1 from './components/Test1';


class App extends Component {
  render() {
    return (
      <Router>
        <div>
          <Navbar />
        <Switch>
          <Route exact path="/" component={Home} />
          <Route exact path="/home" component={Home} />
          <Route exact path="/test" component={Test} />
          <Route exact path="/test1" component={Test1} />
          <Route component={NoMatch} />
        </Switch>
        </div>
      </Router>
    );
  }
}

export default App;

【问题讨论】:

    标签: html reactjs routes


    【解决方案1】:

    我想你不在根目录下时进行刷新。

    我在我的应用程序中使用此 .htaccess 文件以使路由正常工作

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
    

    【讨论】:

    • 在运行 npm run build 之前,您是否将其放入公用文件夹?还是之后添加?
    • 没关系 :)))
    • 仍然不高兴..我只是得到 Not Found The requested URL /test was not found on this server.
    【解决方案2】:

    我们遇到了这个问题,我们使用了以下设置:

    <VirtualHost *:8080>
      ServerName example.com
      DocumentRoot /var/www/httpd/example.com
    
      <Directory "/var/www/httpd/example.com">
        ...
    
        RewriteEngine on
        # Don't rewrite files or directories
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^ - [L]
        # Rewrite everything else to index.html to allow html5 state links
        RewriteRule ^ index.html [L]
      </Directory>
    </VirtualHost>
    

    我们从here获得了这个配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-15
      • 2012-02-01
      • 2021-12-31
      • 2019-01-22
      • 2020-08-15
      • 1970-01-01
      • 2011-05-13
      相关资源
      最近更新 更多