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

前端 未结 30 2650
时光取名叫无心
时光取名叫无心 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:38

    This topic is a little bit old and solved but I would like to suggest you a simply, clear and better solution. It works if you use web server.

    Each web server has an ability to redirect the user to an error page in case of http 404. To solve this issue you need to redirect user to the index page.

    If you use Java base server (tomcat or any java application server) the solution could be the following:

    web.xml:

    
    
    
        
        
            index.jsp
        
    
        
        
            404
            /index.jsp
        
    
    
    

    Example:

    • GET http://example.com/about
    • Web server throws http 404 because this page does not exist on the server side
    • the error page configuration tells to the server that send the index.jsp page back to the user
    • then JS will do the rest of the job on the clien side because the url on the client side is still http://example.com/about.

    That is it, no more magic needs:)

提交回复
热议问题