Deny direct access to all .php files except index.php

Deny direct access to all .php files except index.php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Securing PHP Directives: Denying Access to All Files Except index.php

Restricting direct access to your application's backend logic is a fundamental security practice. You want users to interact only through your entry point, typically index.php, while other files containing business logic or data handling remain accessible solely via include or require. Attempting to manage this with individual .htaccess rules for every file in a large folder structure is unsustainable and prone to human error. The solution lies in leveraging Apache's rewrite engine to enforce a global rule set that denies all .php requests except the specific entry point.

First, ensure your server has the mod_rewrite module enabled. If you are using Apache 2.4 or later, add an <IfModule mod_rewrite.c> block to your .htaccess file located in the root of your protected folder (e.g., myfolder). The logic relies on rewriting all incoming requests to a single handler script while validating the requested path against a whitelist.

Here is a robust pattern that denies direct access to any .php file except index.php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path/to/index.php [L,QSA]

However, this basic snippet simply forwards everything to index.php unless it is an existing file. To strictly deny access to other .php files while serving static assets (CSS, JS, images) correctly, you need a more granular approach using negative lookahead or specific conditionals. A cleaner strategy for Apache 2.4+ involves checking the file extension before allowing the request to proceed to the entry point.

RewriteEngine On

# Deny all .php files that are not index.php
RewriteCond %{REQUEST_URI} \.(php)$ [NC,OR]
RewriteCond %{REQUEST_URI} !^/path/to/index\.php$
RewriteRule ^ - [R=403,L]

# Allow everything else to pass through to existing resources or the front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path/to/index.php [L,QSA]

This configuration explicitly checks if the requested URI ends with .php. If it does, and the URI is not index.php, it returns a 403 Forbidden error immediately. The [R=403,L] flags ensure the client receives the rejection code and no further processing occurs. The subsequent rules handle non-HTML requests by routing them to your main entry point if they aren't static files, preserving standard behavior for assets like images or CSS while keeping your logic hidden.

A critical consideration when implementing this is the distinction between "denying access" and "ignoring the file." If a user tries to load config.php directly, returning a 403 error provides information that might be useful if you are not careful with error handling. Ensure your PHP configuration logs these attempts appropriately so you can monitor for brute-force attempts against sensitive files like .env or database credentials.

If you prefer a rule set that applies recursively to subfolders within myfolder, the rules must reside in the .htaccess file of the parent directory (myfolder). Apache processes directives hierarchically, but rewriting logic is generally most effective when defined at the highest possible level where the scope is known. Placing these rules in the root of your project and targeting specific paths is often cleaner than scattering them across subdirectories.

For Laravel applications specifically, you might find this pattern useful within the public/.htaccess file or the .env configuration if you are deploying to a managed host. The principle remains the same: force all dynamic requests through the bootstrap process defined in your entry point, while blocking direct access to any script that isn't explicitly whitelisted as the public face of your application.

Remember that this approach assumes index.php is always served at its canonical path. If you have aliases or redirects pointing to other filenames, you must update the regex condition to account for those variations to avoid accidental 403 errors on valid routes.

Our network