A CentOS 7 server hosts a public front end web site at example.com, and also hosts a backend API at home.example.com. The VirtualHost records below successfully direct requests for example.com to the front end while directing requests for the backend to home.example.com. However, requests for arbitrary requests to paths on example.com are given 404 errors.

What specific changes need to be made to the VirtualHost config below so that requests to example.com/anyPath get redirected to example.com?

For example, a request for http://example.com/home that reaches the server should be returned with example.com

Here is the VirtualHost:

<VirtualHost www.example.com:80>
    ServerName www.example.com
    ServerAlias example.com
    ErrorLog /var/log/httpd/example_com_error.log
    CustomLog /var/log/httpd/example_com_requests.log combined
    DocumentRoot /var/www/example/public_html

<VirtualHost home.example.com:80>
    ServerName home.example.com
    ErrorLog /var/log/httpd/example_home_com_error.log
    CustomLog /var/log/httpd/example_home_com_requests.log combined
    ProxyPass / http://public.ip.for.api:1234/ connectiontimeout=5 timeout=30

2 Answers 2


If you want to redirect, you can add this to your first VirtualHost directive (using the appropriate index file).

RedirectMatch 302 ^/(?!index.html$).+ http://example.com

Basically this means you are matching anything doesn't match /index.html exactly and is followed by one or more characters.

You can also use Rewrite for a different effect. No matter what is requested Apache will serve up the index file.

RewriteEngine on
RewriteRule ^/.* index.html

The simplest, cleanest and — imo — most correct way of achieving this result is:

<VirtualHost www.example.com:80>
    DirectoryIndex index.html     # This is likely already set globally
    FallbackResource /index.html  # Catchall for any undefined path (from Apache's point of view)

One great advantage of FallbackResource over RedirectMatch and the mod_rewrite-based approach is that it is a set-once-and-forget setting that keeps working when new routes are added, without having to update this configuration. It's also SPA-friendly — unlike redirections — as it preserves the path of the original request. It essentially tells Apache to let the fallback resource handle any request for routes that are undefined from Apache's point-of-view.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .