【问题标题】:React router dom not working after deployment部署后反应路由器dom不起作用
【发布时间】:2021-09-08 06:52:31
【问题描述】:

我正在使用 react 路由器 dom 进行前端路由,但是每当我刷新页面或访问任何不是根目录或“/”的 URL 时,服务器都会在我运行 npm build 路由器之前以 404 响应即使我刷新页面也能正常工作。

【问题讨论】:

    标签: reactjs rxjs react-router http-status-code-404 react-router-dom


    【解决方案1】:
    【解决方案2】:

    您应该使用以下代码(强制 https)将 .htaccess(服务器配置)文件添加到远程 Apache 服务器目录:

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTPS} off [OR]
      RewriteCond %{HTTP_HOST} ^www\. [NC]
      RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
      RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [NC,L]
      RewriteRule ^(.*) /index.html [NC,L]
    </IfModule>
    

    不强制https:

    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]
    

    【讨论】:

    • 我正在使用 nginx
    猜你喜欢
    • 2019-02-04
    • 2017-09-10
    • 2021-12-21
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    相关资源
    最近更新 更多