在 Apache 服务器上处理 React Router [英] Dealing with React Router on Apache server

查看:59
本文介绍了在 Apache 服务器上处理 React Router的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为 Apache 服务器上的 React 生成的 URL 路径重定向 404 错误时遇到问题.

I am experiencing problems with redirecting 404 errors for URL paths generated by React on an Apache server.

场景:

假设我有一些由 React Router 生成的 URL:www.somewebsite.com/apps

Let's say I have some URL generated by React Router: www.somewebsite.com/apps

问题:

如果最终用户点击刷新,我如何重定向回 www.somewebsite.com/apps?

How do I redirect back to www.somewebsite.com/apps if the end-user clicks on refresh?

我尝试过的:

到目前为止,我能够将用户重定向到 www.somewebsite.com.我只需要在重定向之前获取路径.

So far, I am able to redirect the user to www.somewebsite.com. I just need to get the path before the redirect.

推荐答案

设置 Apache 重写规则以重写对 React 应用程序 (index.html) 的所有请求.

Set an Apache rewrite rule to rewrite all requests to your React app (index.html).

将其放入您的 vhost 配置或 .htaccess 文件中.

Put this in your vhost config or a .htaccess file.

<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]
</IfModule>

现在,当用户在诸如 /apps 之类的任何路由上按下刷新(或直接访问)时,您的 React 应用程序将被加载并且路由器将显示相关路由.

Now, when a user presses refresh (or visits directly) on any route like /apps, your React app will be loaded and the router will display the relevant route.

注意:RewriteCond 将静态文件排除在必要的重写之外.

Note: the RewriteCond excludes static files from being rewritten which is necessary.

这篇关于在 Apache 服务器上处理 React Router的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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