Passing the URL path to index.php on Ubuntu

by | Oct 16, 2026 | Tutorials | 0 comments

Quick answer

Modern PHP apps expect every request to hit a single front controller (index.php), with the real route carried in the URL.

Modern PHP apps expect every request to hit a single front controller (index.php), with the real route carried in the URL. Apache needs a rewrite rule to make that happen.

What you need: An Ubuntu server running Apache with mod_rewrite available, plus a vhost or .htaccess you can edit.

Enable mod_rewrite

Run a2enmod rewrite and restart Apache. Without this module the rules are silently ignored.

Allow overrides

The vhost (or the default /etc/apache2/apache2.conf for /var/www) must have AllowOverride All, otherwise .htaccess is not read.

Write the front-controller rule

Redirect anything that is not a real file or directory to index.php, preserving the path in a query string or via PATH_INFO.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

How it works, in detail

Front controllers work because the web server sends every non-file request to a single script and lets the application read the path from the request. On Apache that means a rewrite condition that skips real files and directories, then a rule that hands everything else to index.php. Getting AllowOverride right matters because a rule that is never read fails silently, leaving you with 404s or a loop. Once the rewrite works, the framework’s router takes over and parses the URL itself.

Common pitfalls

  • Forgetting a2enmod rewrite, so the rules are ignored without any error.
  • Leaving AllowOverride None, which prevents .htaccess from being loaded.

Pro tip

Add a RewriteLog or use apachectl -S to confirm the vhost is loading the rules before blaming the application.

This is the same pattern Laravel, Symfony and Slim use. Get it right once and every routing framework on the box just works.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

avatart

About

I’m a System Administrator and a developer with 10+ years of experience.