How to configure apache web server to deploy laravel 5

How to configure apache web server to deploy laravel 5

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

Stefan Bogdanescu

Founder & Senior Architect

Configuring Apache for Laravel 5: Resolving the 404 on Sub-Routes

You can access your root route, but clicking a link to /link results in a 404 error. This behavior indicates that Apache is serving the raw PHP file structure rather than routing requests through Laravel's public/index.php. The browser correctly resolves your view to localhost/mylaravelapp/public/link, meaning the URL is being formed as expected, but the server is not intercepting it.

To understand why this happens, you must look at how Apache handles .php extensions. By default, Apache maps a request for /public/link directly to public/link.php. Since your Laravel application does not have a file named link.php, Apache returns a 404 before the request ever reaches index.php.

The solution involves configuring the mod_rewrite module to intercept all requests and redirect them to index.php. While you mentioned enabling this via a2enmod mod_rewrite, that command only activates the module globally; it does not configure the specific rules required for Laravel. You must apply these directives within a virtual host configuration or a specific .htaccess file located in your project's root directory.

Configuring the Virtual Host

The most robust approach is to place the rewrite rules directly in your Apache virtual host definition, typically found in /etc/apache2/sites-available/your-site.conf. Ensure that mod_rewrite and mod_dir are enabled using:

sudo a2enmod rewrite
sudo a2dismod dir

Disabling mod_dir is critical because it prevents Apache from automatically serving directory listings or finding files outside of the defined root, which can interfere with Laravel's routing logic.

Add the following block inside your <VirtualHost> tags:

<Directory /path/to/your/project>
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

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

The AllowOverride All directive is essential here; it tells Apache to process .htaccess files if you choose to use them later. The rewrite rule itself checks two conditions: the requested resource must not be an existing directory (!-d) or an existing file (!-f). If neither condition matches, the request is rewritten to /public/index.php. The [L] flag ensures this is the last rule processed, preventing further rewriting.

Using .htaccess in the Project Root

If you prefer not to modify the global Apache configuration, you can achieve the same result using an .htaccess file placed in your project's root (not inside the public folder).

Create a file named .htaccess in your project directory with the following content:

RewriteEngine On
RewriteBase /
# If the requested file or directory exists, serve it normally
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Otherwise, send everything to index.php
RewriteRule (.*) /public/index.php?_route_=/$1 [QSA,L]

Note the use of RewriteBase. This tells Apache what the base URL of your application is relative to the server root. If your project is served from the domain root, this might remain /, but if it is in a subdirectory like /mylaravelapp, you must adjust this value accordingly.

The public Directory Quirk

A common pitfall when deploying Laravel on Apache involves the public directory itself. In older Laravel versions or specific configurations, users mistakenly set the DocumentRoot to the project root instead of the public folder. If your DocumentRoot is set to /path/to/your/project, the URL /link points directly to a non-existent file. By rewriting the request to /public/index.php, you ensure that Laravel handles the routing logic, regardless of whether the initial URL was intended for the root or a sub-route.

This configuration ensures that every incoming request is treated as a route defined in your routes/web.php file, allowing middleware and controllers to function exactly as they would on a local development server running PHP's built-in server.

Our network