Laravel 4 removing public from URL

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Laravel Routing: Understanding and Managing the /public Directory

As developers diving into the world of the Laravel framework, one of the initial points of confusion often revolves around the file structure. Specifically, why does everything seem to live inside a public folder, and how do we manage access to our application files?

This post addresses a common scenario: wanting to access a Laravel application directly at a cleaner URL, such as localhost/laravel/about instead of the conventional route like localhost/laravel/public/about. We will explore the intended structure, the best practices, and the technical methods you can use to achieve your desired URL structure.

The Laravel Foundation: Why the /public Folder Exists

When you set up a standard Laravel project, the framework deliberately places all publicly accessible assets—such as CSS files, JavaScript, images, and the main entry point for web requests—inside the public directory. This is not arbitrary; it’s a deliberate architectural choice rooted in security and performance.

The public folder serves as the document root for your web server (like Apache or Nginx). By making this folder public, Laravel ensures that only files intended for direct access are exposed. Any code placed outside this directory, such as configuration files, models, controllers, and routes, remains protected from direct web access, significantly enhancing security. This principle is fundamental to how modern, robust frameworks like Laravel operate, emphasizing separation of concerns.

For more detailed insights into the architecture and design philosophy behind these powerful tools, understanding the core principles outlined by resources like Laravel Company is invaluable.

Solution 1: The Recommended Approach (Standard Routing)

The most secure and maintainable way to run a Laravel application is to adhere to its structure. When using a local server setup like XAMPP, the correct access path mirrors the file structure:

Correct Access: localhost/laravel/public/about

Laravel’s routing system handles mapping this request correctly. The web server serves files from the document root (public), and Laravel's bootstrap process handles mapping the URL path to the corresponding controller method. Trying to bypass this structure often leads to configuration headaches or security vulnerabilities if not handled by a proper framework layer.

Solution 2: Advanced Server Configuration for URL Rewriting

If your requirement is strictly to access the application root without explicitly typing /public in the URL—for example, achieving localhost/laravel/about—this requires configuring your web server (like Apache) to perform URL rewriting internally. This is typically achieved using .htaccess files or Virtual Host configurations, rather than altering Laravel’s core structure.

For a standard Apache setup running XAMPP, you would place an .htaccess file in the root directory of your application (the parent folder containing public). This file uses the mod_rewrite module to internally redirect the request from the URL path to the correct file within the public directory.

Here is a conceptual example of what such an .htaccess file might look like, designed to handle routing for a root-level application:

apache
# .htaccess file in the root folder (e.g., /laravel/)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]

Explanation of the Code:

  1. RewriteEngine On: Activates the URL rewriting engine.
  2. RewriteCond %{REQUEST_FILENAME} !-f: Checks if the requested path is not an existing file.
  3. RewriteCond %{REQUEST_FILENAME} !-d: Checks if the requested path is not an existing directory.
  4. RewriteRule ^(.*)$ public/$1 [L]: If neither condition above is met (meaning it’s a request for a route), rewrite the entire request (^(.*)$) by prepending public/ to the original path, and stop processing further rules ([L]).

Important Caveat: While this .htaccess trick can achieve the URL manipulation you desire, it is an advanced technique. In complex enterprise environments, relying on framework-level routing (like Laravel’s built-in routing) combined with proper server configuration (Virtual Hosts) is generally preferred over direct file system manipulation for ensuring stability and security.

Conclusion

To summarize, while the convention in the Laravel ecosystem dictates accessing files via /public, you have technical options to manipulate the URL structure using web server directives like .htaccess. For a beginner learning the framework, sticking to the standard structure (/public) is the most robust path. If you are building highly customized deployments or managing complex server setups, understanding how to use mod_rewrite provides the necessary power to control your application's public-facing URLs precisely. Always prioritize security and maintainability when implementing these changes.