Deny direct access to a folder and file by htaccess

Deny direct access to a folder and file by htaccess

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

Stefan Bogdanescu

Founder & Senior Architect

Deny Direct Access to a Folder and File by htaccess

Protecting internal application logic from direct browser access is a fundamental security posture for any PHP application. While you can rely on server-side checks inside every include statement, that approach is fragile; if a developer forgets a guard clause or if an attacker bypasses your logic, the sensitive files are exposed. The .htaccess file offers a more robust solution by enforcing access control at the web server level before the PHP engine even initializes.

In your specific scenario, you have three distinct entities: index.php in the root (your entry point), an includes/ folder containing shared logic, and submit.php handling form submissions. The goal is to ensure that include() calls within index.php function correctly while blocking any attempt to navigate directly to /includes/somepage.php or /submit.php.

Restricting the Includes Folder

The most straightforward method to protect the includes/ folder is to deny all access explicitly. This creates a hard barrier that returns a 403 Forbidden status immediately. Since PHP files inside this directory are never intended to be executed directly, blocking them at the Apache level prevents unnecessary parsing overhead and eliminates the risk of logic bypasses.

Add the following rules to your .htaccess file, typically located in the root of your project or specifically within the includes/ directory depending on your deployment strategy:

<FilesMatch "\.(php|php5|phtml)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

This rule targets only PHP scripts within that specific location. It ensures that even if a user guesses the filename includes/config.php or includes/functions.php, Apache rejects the request outright. The server does not look for an error page by default, so users simply see a raw "403 Forbidden" header. If you prefer to return a custom HTML error page instead of the raw status code, you would need to configure a custom error document in your main .htaccess file or rely on mod_rewrite with a rewrite rule pointing to a generic 403 handler.

Protecting submit.php

The logic for submit.php requires slightly different handling because it is a legitimate endpoint, just not one accessible via direct URL navigation from the public. You want users to reach it only through the form action defined in your views or internal redirects, not by typing the URL into the browser.

To achieve this, you can use a combination of Allow and Deny directives based on IP addresses, User-Agent strings, or simply deny all access if you intend for this file to be completely hidden from external browsers. However, since submit.php likely needs to be called via HTTP POST (perhaps triggered by AJAX or standard form submission), a total "deny all" approach might break your application if the form action points directly to that URL.

If submit.php is indeed a public-facing endpoint but you want to prevent users from clicking a link to it or bookmarking it, denying direct access via GET while allowing POST is complex with standard .htaccess without modules like mod_rewrite. A simpler and often more secure pattern for sensitive handlers is to deny all access and rely on your application code to handle the logic. If you must allow the form submission, ensure your view templates do not generate a direct <a href="submit.php"> link, and that your form action points to this file.

If you decide to restrict access entirely regardless of the HTTP method, apply this rule specifically to submit.php:

<Files "submit.php">
    Order Allow,Deny
    Deny from all
</Files>

Verifying Internal Inclusion Works Correctly

The critical test here is ensuring that include statements in index.php continue to function. When your application runs include 'includes/somepage.php';, the PHP engine resolves this as a server-side include operation, not an HTTP request. The web server never receives a GET request for /includes/somepage.php. Therefore, the rules defined in .htaccess that block direct URL access do not interfere with internal includes.

However, there is a common pitfall involving relative paths versus absolute filesystem paths. If index.php uses a relative path like include 'includes/somepage.php', ensure your includes/ folder is physically located within the web root of your project. If you move the project structure or use a virtual host where the document root differs, the include might fail with an "Include failed: Failed to open stream" error, which is distinct from an access denial but worth troubleshooting during deployment.

Final Considerations on Error Handling

When denying access via .htaccess, users typically encounter a raw 403 error page generated by the server configuration (often a generic HTML string provided by Apache or Nginx). For a professional experience, it is best practice to configure a custom error document. In your root .htaccess, you can define:

ErrorDocument 403 /errors/403.php

This ensures that whenever the includes/ folder or protected files are accessed directly, users are redirected to a friendly page explaining that access is restricted, rather than seeing server configuration details. This maintains user trust without exposing the existence of your internal folders.

Our network