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 apache2orsudo systemctl reload httpd) - Access to the Apache error log (usually
/var/log/apache2/error.logor/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
- Reproduce the problem and note the exact response. Open a browser and request a non-PHP file such as
http://yoursite/style.cssorhttp://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. - Check the DocumentRoot. Open the vhost file and confirm the
DocumentRootline points to the folder that actually contains your site files. A common mistake is pointing it at a subfolder that only holds.phpscripts. The path must exist and be readable by the Apache user (oftenwww-dataorapache). - 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. Ifindex.phpis listed butindex.htmlis not, requests for the bare domain will fall through to PHP handling only, which can look like "only PHP works." - Look for restrictive Directory or FilesMatch blocks. Search the vhost for
<Directory,<Files,<FilesMatch, andRequire. A line likeRequire all deniedoutside 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. - Check for rewrite rules that exclude non-PHP files. If
mod_rewriteis enabled, look for rules that redirect everything toindex.phpbut skip existing files. A missingRewriteCond %{REQUEST_FILENAME} !-fline means real files get rewritten away. Add the condition so existing files are served directly. - Verify file permissions and ownership. Static files must be readable by the Apache user. Run
ls -lin the DocumentRoot. Files should typically be644and directories755, owned by your deploy user with the Apache group. A600permission on a CSS file will produce a 403 even with perfect config. - Confirm the module for static files is loaded. On most installs
mod_mimehandles file types automatically, but if it has been disabled, Apache may not know how to serve.cssor.js. Check withapache2ctl -M(Debian/Ubuntu) orhttpd -M(RHEL) and enable it if missing. - Test the config before reloading. Run
sudo apache2ctl configtestorsudo httpd -t. Fix any syntax errors it reports. A broken config can silently keep the old vhost active. - Reload Apache and retest. Use
sudo systemctl reload apache2(orhttpd). 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 -Swhich vhost actually handles the request. - Forgetting to enable the site with
a2ensiteon Debian/Ubuntu, so the edited file is never loaded. - Putting
DirectoryIndexoutside the<Directory>block, where it may be overridden. - Leaving a stray
Require all deniedfrom 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
ServerNameso you can identify it in logs. - Use
LogLevel debugtemporarily in the vhost while diagnosing, then set it back towarn. - Back up the vhost file before editing so you can restore a working state quickly.
- Test with
curl -I http://yoursite/style.cssto 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).