Configure Apache to redirect all requests to index.html?

5,849

I do mine with a .htaccess file. In the DocumentRoot for the site, I create the .htaccess file and it in goes

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

Per @StephenOstermiller 's comment below, you also need to make sure that AllowOverride All is set in the Directory stanza as well. While mine is set up that way, I forgot to add that in the post since it is part of my default config.... so @StephenOstermiller gets the credit for that part.

Share:
5,849

Related videos on Youtube

Maruli
Author by

Maruli

Updated on September 18, 2022

Comments

  • Maruli
    Maruli almost 2 years

    A CentOS 7 server is hosting a web application in /var/www/someapp/public_html. Requests for mydomain.com get served the file located at /var/www/someapp/public_html/index.html. The user can then navigate to several browser-routes that are defined in the JavaScript web application. However, when the user refreshes the browser from one of those UI-routes, apache replies with a message indicating that the requested url is not found on the server.

    What specific config settings need to be changed in order for all routes sent to apache to be redirected to index.html?

  • Maruli
    Maruli about 7 years
    I created a .htaccess file in the DocumentRoot for the site in question, and put your exact syntax in it. I then typed systemctl restart httpd.service, and I waited a couple minutes. Then I reloaded the web site and refreshed one of the views, only to have the same problem persist. What else do I need to change? Perhaps something else in httpd.conf?
  • Stephen Ostermiller
    Stephen Ostermiller about 7 years
    You need to enable .htaccess by making sure that AllowOverride All is set for the directory in the Apache config files.
  • Maruli
    Maruli about 7 years
    @StephenOstermiller Your suggestion enabled this to work. Therefore, if you write up an answer, I would be happy to mark it as accepted and +1.
  • Stephen Ostermiller
    Stephen Ostermiller about 7 years
    You can mark this one. It has the meat of it.
  • Maruli
    Maruli about 7 years
    @StephenOstermiller Meat alone gives people heart attacks, diabetes, and strokes. Vegetables are required. Your comments added the required vegetables.