Apache Mod Rewrite For Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering URL Rewriting: Solving Pathing Issues in Laravel with Apache Mod Rewrite
As a senior developer, I frequently encounter situations where the beautiful structure of a modern framework like Laravel collides with the realities of server configuration, especially when dealing with local development environments like Wampserver. The issue you are facing—where URLs become overly verbose (like `http://localhost/laravel/public/index.php/home/index`) and how to strip the unnecessary directory structure—is a classic problem solved by mastering Apache’s `mod_rewrite`.
This post will dive deep into why this happens in Laravel, analyze your existing `.htaccess` attempt, and provide the definitive solution to achieve clean, RESTful URLs directly from your base path.
## Understanding the Laravel Structure and Routing
Before diving into the code, it's crucial to understand how Laravel manages requests. Laravel enforces a strict MVC (Model-View-Controller) structure where all public-facing assets reside in the `/public` directory. The entry point for all requests is `index.php` inside that folder.
When you see URLs like `http://localhost/laravel/public/index.php/home/index`, it indicates that your web server (Apache) is correctly routing the request to the physical file, but the URL itself contains redundant path segments (`/public/index.php`). Our goal with Mod Rewrite is to intercept this request and redirect it internally so Laravel only sees the clean segment (e.g., `/home`) while still executing the necessary framework logic.
## Deconstructing Your `.htaccess` Attempt
Your initial attempt using `mod_rewrite` was a good start, but it was structured more for internal routing within a single application context rather than stripping the external public path elements you are seeing in your full URL.
```apache
Options +FollowSymLinks
Options -indexes
DirectoryIndex index.PHP
RewriteEngine on
RewriteCond $1 !^(index\.PHP|images|robots.txt)
RewriteCond %{REQUEST_ FILENAME} !-f
RewriteCond %{REQUEST_ FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L, QSA]
```
This rule essentially tells Apache: "Take anything after the base path and append it to `index.php/`." While this is vital for Laravel's internal routing mechanism (allowing routes defined in `routes/web.php` to work), it doesn't solve the external URL cleanup you desire. We need a slightly different approach to strip the `/public/` part from the URL structure itself.
## The Solution: Rewriting the Base Path
To achieve the desired clean URL structure—where `/home/index` maps directly to your application’s logic—we need to instruct Apache to ignore the intermediary directory structure and map the request directly to the Laravel entry point.
Assuming your project root is `C:\wamp\www\laravel`, the `.htaccess` file should be placed in the **project root** (i.e., inside the `laravel` folder, or perhaps the `public` folder if you configure the server accordingly).
The most effective way to handle this involves ensuring that requests are routed through the public entry point while allowing clean URLs. Since Laravel strongly emphasizes using its built-in routing system, we focus on making the rewrite seamless for the framework.
Here is the corrected and simplified approach to achieve URL shortening:
```apache