How to setup apache server for React route?

前端 未结 9 1092
无人共我
无人共我 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:51

    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

    
        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]
    
    

    to the above mentioned file under

提交回复
热议问题