.htaccess redirect from site root to public folder, hiding "public" in URL?

.htaccess redirect from site root to public folder, hiding "public" in URL?

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

Stefan Bogdanescu

Founder & Senior Architect

Redirecting Site Root to a public Folder Without Exposing the Path

The directory structure you described places your application entry point inside a subdirectory named public. This is a common pattern, often adopted from Laravel or Symfony conventions where that folder acts as the web root while containing sensitive source code in sibling directories like src. The goal here is straightforward: serve requests to https://my-project.com but physically route them to My-Project/public/index.php, ensuring the src directory remains inaccessible via HTTP.

To achieve this, you need a rewrite rule in your .htaccess file located at the root of your project (My-Project). The core mechanism relies on Apache's mod_rewrite module. When a request hits the server for My-Project.com, Apache receives a URL path of /. You want to internally rewrite this request to /public/index.php.

Here is the standard configuration to handle this scenario:

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Redirect all requests to the public folder, excluding assets that should be served directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /public/index.php [L,QSA]
</IfModule>

The logic behind this rule is critical. The RewriteCond lines check if the requested path corresponds to an existing file (-f) or directory (-d). If a static asset like an image in /public/img/logo.png is requested, the condition fails because that file actually exists on the disk; Apache serves it directly without rewriting. However, if a user requests the root My-Project.com, no file named index exists at the root level. Consequently, the rule triggers, rewriting the URL to My-Project/public/index.php.

This internal rewrite is transparent to the browser. The address bar remains My-project.com, but your application in public/index.php receives the request as if it were accessed directly. Inside your PHP file, you can use $this->request->basePath() or similar routing helpers to determine the current path relative to the public folder, ensuring your application logic doesn't break due to the underlying URL structure.

Security is the primary driver for this architecture. By forcing all traffic through public/index.php, you ensure that files in the src directory—containing backend logic and templates—are never served directly by Apache. Without this redirect, a user could simply navigate to My-Project/src/config.php or My-Project/templates/view.blade.php. Even if these files aren't executable, exposing them leaks your internal file structure and potentially sensitive configuration details.

There are specific pitfalls to avoid when implementing this. First, ensure that the public folder itself contains its own .htaccess file if you need to block access to specific subdirectories within it, such as a debug folder or vendor cache. Do not rely on the root rule to restrict access inside the public folder; Apache handles static assets there before the rewrite engine processes them.

Another consideration is virtual hosting. If you are using name-based virtual hosting, make sure your DocumentRoot in the server configuration points to My-Project/public, not My-Project. If the DocumentRoot is set incorrectly at the Apache level, the .htaccess rules inside My-Project will behave unexpectedly because the physical path served by the server changes based on the virtual host configuration.

If you encounter issues where static assets fail to load, double-check that your public folder has its own .htaccess allowing public access to assets before the root redirect logic interferes. Sometimes, overly broad rewrite rules at the root can inadvertently capture requests for files that exist in subdirectories of the public folder if not ordered correctly or if the conditions are too loose.

For those managing multiple projects with similar structures, Laravel offers a robust solution out of the box. Their documentation details how to configure this pattern effectively, often serving as a reference for custom implementations. You can find detailed guidance on setting up these directory structures and security configurations at https://laravelcompany.com.

Our network