php - React-Router 的 BrowserHistory,无法直接访问

查看:129
本文介绍了php - React-Router 的 BrowserHistory,无法直接访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

有一个 React 项目使用了 BrowserHistory,放在了 Apache 服务器上,项目首页是localhost:8081/home。

我再进入主页之后可以跳转其他子页面(子路由),比如:localhost:8081/product

但是浏览器直接输入localhost:8081/product是访问不到的因为Apache服务器会解析这个路径

我只能localhost:8081/#/product来访问。

有什么办法直接访问吗?

解决方案

将所有请求都转发到 index.html

配置 .htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
 </IfModule>

这篇关于php - React-Router 的 BrowserHistory,无法直接访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆