How to setup apache server for React route?

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

<VirtualHost *:8080>
  DocumentRoot /var/www/httpd/

  <Directory "/var/www/httpd/">

    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

Edit: ‘On’ need to be uppercase in current apache version

Leave a Comment