How to setup apache server for React route?

前端 未结 9 1094
无人共我
无人共我 2020-12-08 10:04

I have my react app running great on my local dev server but it did not work when I dump my production ready files straight into Apache\'s htdocs directory:

Here is

9条回答
  •  生来不讨喜
    2020-12-08 10:43

    Change the VirtualHost configuration (typically found in /etc/httpd/conf.d\vhosts.conf) by adding the following Rewrite* lines:

    
      ServerName example.com
      DocumentRoot /var/www/httpd/example.com
    
      
        ...
    
        RewriteEngine on
        # Don't rewrite files or directories
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^ - [L]
        # Rewrite everything else to index.html to allow html5 state links
        RewriteRule ^ index.html [L]
      
    
    

    This tells Apache to serve any files that exist, but if they don't exist, just serve /index.html rather than a 404: not found.

    • Apache Reference: Configuring Apache Virtual Hosts

    • react-router History Reference: Configuring Your Server

    Complete answer gratefully stolen from here

提交回复
热议问题