How to setup apache server for React route? How to setup apache server for React route? apache apache

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>  ServerName example.com  DocumentRoot /var/www/httpd/example.com  <Directory "/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]  </Directory></VirtualHost>

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.

Complete answer gratefully stolen from here


The above solution works for Ubuntu as well but I have struggled a bit with it so here are the steps necessary to make it work.

Location of the file where you need to place the above mentioned configuration is under

/etc/apache2/sites-enabled

default is

/etc/apache2/sites-enabled/000-default.conf

Then you need to make sure that RewriteEngine is running (otherwise you will get an error when restarting Apache server).

sudo a2enmod rewrite

Finally, restart Apache server

sudo /etc/init.d/apache2 restart

Now, it should work.

When you are using default configuration (root of the website is under /var/www/html), then all you need to do is to place

<Directory "/var/www/html">    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]</Directory>

to the above mentioned file under <VirtualHost ...>


If you have to use .htaccess and a sub directory then following works for me.

Options -MultiViewsRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^ index.html [QSA,L]