If your Apache virtual host is only serving PHP files and returns a 403 or 404 for everything else, the cause is almost always a missing or wrong DirectoryIndex directive, a broken DocumentRoot, or an <FilesMatch>/<Directory> block that restricts access to non-PHP files. The fix is to confirm the vhost's DocumentRoot points at the right folder, add a DirectoryIndex line (for example DirectoryIndex index.php index.html), and check that no Require all denied or rewrite rule is blocking static assets. Restart Apache after each change. The steps below walk through that diagnosis and repair in order.

What You'll Need

  • Shell or SSH access to the server (or local admin rights on Windows)
  • The path to your Apache config file (commonly /etc/apache2/sites-available/ on Debian/Ubuntu or /etc/httpd/conf.d/ on RHEL/CentOS)
  • A text editor such as nano, vim, or Notepad++
  • The ability to restart or reload Apache (sudo systemctl reload apache2 or sudo systemctl reload httpd)
  • Access to the Apache error log (usually /var/log/apache2/error.log or /var/log/httpd/error_log)

Note: exact file paths and directive names can differ slightly by distribution and Apache version; if a path below does not exist, check your distro's documentation for the equivalent location.

Step-by-Step

  1. Reproduce the problem and note the exact response. Open a browser and request a non-PHP file such as http://yoursite/style.css or http://yoursite/logo.png. A 403 means access is denied by config; a 404 means Apache is looking in the wrong folder. Write down which one you see, because it points to different fixes.
  2. Check the DocumentRoot. Open the vhost file and confirm the DocumentRoot line points to the folder that actually contains your site files. A common mistake is pointing it at a subfolder that only holds .php scripts. The path must exist and be readable by the Apache user (often www-data or apache).
  3. Add or fix the DirectoryIndex line. Inside the <Directory> block for your DocumentRoot, add:
    DirectoryIndex index.php index.html index.htm
    This tells Apache which file to serve when a directory is requested. If index.php is listed but index.html is not, requests for the bare domain will fall through to PHP handling only, which can look like "only PHP works."
  4. Look for restrictive Directory or FilesMatch blocks. Search the vhost for <Directory, <Files, <FilesMatch, and Require. A line like Require all denied outside a PHP-specific block, or a <FilesMatch> that only allows \.php$, will block everything else. Remove or correct the restriction so static files are allowed.
  5. Check for rewrite rules that exclude non-PHP files. If mod_rewrite is enabled, look for rules that redirect everything to index.php but skip existing files. A missing RewriteCond %{REQUEST_FILENAME} !-f line means real files get rewritten away. Add the condition so existing files are served directly.
  6. Verify file permissions and ownership. Static files must be readable by the Apache user. Run ls -l in the DocumentRoot. Files should typically be 644 and directories 755, owned by your deploy user with the Apache group. A 600 permission on a CSS file will produce a 403 even with perfect config.
  7. Confirm the module for static files is loaded. On most installs mod_mime handles file types automatically, but if it has been disabled, Apache may not know how to serve .css or .js. Check with apache2ctl -M (Debian/Ubuntu) or httpd -M (RHEL) and enable it if missing.
  8. Test the config before reloading. Run sudo apache2ctl configtest or sudo httpd -t. Fix any syntax errors it reports. A broken config can silently keep the old vhost active.
  9. Reload Apache and retest. Use sudo systemctl reload apache2 (or httpd). Then request the same static file again. If it still fails, tail the error log while making the request: sudo tail -f /var/log/apache2/error.log. The log line will name the exact file and reason.

Common Mistakes

  • Editing the wrong vhost file — many servers have a default vhost that takes precedence. Confirm with apache2ctl -S which vhost actually handles the request.
  • Forgetting to enable the site with a2ensite on Debian/Ubuntu, so the edited file is never loaded.
  • Putting DirectoryIndex outside the <Directory> block, where it may be overridden.
  • Leaving a stray Require all denied from a security tutorial in the vhost root.
  • Reloading instead of restarting after a major config change — a reload can leave a broken worker running.
  • Assuming the fix is PHP-related when the actual fault is file permissions or a rewrite rule.

Tips

  • Keep one vhost per site and give each a clear ServerName so you can identify it in logs.
  • Use LogLevel debug temporarily in the vhost while diagnosing, then set it back to warn.
  • Back up the vhost file before editing so you can restore a working state quickly.
  • Test with curl -I http://yoursite/style.css to see the status code and headers without browser caching getting in the way.

Troubleshooting

If static files still fail after the steps above, work from simplest to most invasive. First clear the browser cache or test in a private window, since a cached 403 can persist. Next, confirm the vhost is the one being used with apache2ctl -S. Then check the error log immediately after a request — the message usually names the file and the reason (permission denied, file not found, or client denied by server configuration). If the log says "client denied by server configuration," a Require or <Directory> block is still restrictive; search the entire config tree, not just the vhost, because .htaccess files can also deny access. If it says "file does not exist," the DocumentRoot or an alias is wrong. If permissions are the issue, fix ownership and modes as described in step 6. As a last resort, temporarily disable .htaccess by setting AllowOverride None in the vhost to see whether a hidden rule is the culprit, then re-enable and fix the rule. Do not delete the vhost or reset the server until you have captured the error log line, because that message is the fastest route to the real cause.

Frequently Asked Questions

Why does my vhost serve index.php but not index.html?

Because DirectoryIndex lists index.php but not index.html. Add index.html to the directive and reload Apache.

Can a .htaccess file cause this?

Yes. A Require all denied or a restrictive <FilesMatch> in .htaccess can block static files even when the vhost looks correct. Check that file in the DocumentRoot.

Do I need to restart or just reload Apache?

A reload is enough for most config changes, but if the config test fails or behavior does not change, a full restart is safer. Always run configtest first.

Why do I get 403 on images but PHP pages load fine?

File permissions. Images are often uploaded with restrictive modes. Set them to 644 and ensure the Apache user can read them.

Does this apply to Windows Apache installs?

Yes, the directives are the same; only the file paths differ (for example C:\Apache24\conf\extra\httpd-vhosts.conf).