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

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

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

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

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

有什么办法直接访问吗?

阅读 4k
1 个回答

将所有请求都转发到 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>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题