React-router urls don't work when refreshing or writing manually

前端 未结 30 2634
时光取名叫无心
时光取名叫无心 2020-11-21 05:07

I\'m using React-router and it works fine while I\'m clicking on link buttons, but when I refresh my webpage it does not load what I want.

For instance, I am in

30条回答
  •  有刺的猬
    2020-11-21 05:46

    I used create-react-app to make a website just now and had the same issue presented here. I use BrowserRouting from the react-router-dom package. I am running on a Nginx server and what solved it for me was adding the following to /etc/nginx/yourconfig.conf

    location / {
      if (!-e $request_filename){
        rewrite ^(.*)$ /index.html break;
      }
    }
    

    Which corresponds to adding the following to the .htaccess in case you are running Appache

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

    This also seems to be the solution suggested by Facebook themselves and can be found here

提交回复
热议问题