Skip to main content

Overview

FastRoute requires proper Apache configuration to enable URL rewriting. This ensures that all requests are routed through index.php for proper route handling.

Prerequisites

  • Apache web server installed
  • mod_rewrite module enabled
  • Access to Apache configuration files

Configuration Steps

1

Enable mod_rewrite

First, ensure that Apache’s mod_rewrite module is enabled:
sudo a2enmod rewrite
sudo systemctl restart apache2
2

Configure AllowOverride

Edit your Apache virtual host configuration file to enable .htaccess overrides.Open the configuration file (typically located at /etc/apache2/sites-available/000-default.conf):
sudo nano /etc/apache2/sites-available/000-default.conf
Add or modify the Directory directive to enable AllowOverride:
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Setting AllowOverride All is required for .htaccess files to work. Without this, your rewrite rules will be ignored.
3

Create .htaccess File

Create a .htaccess file in your project root directory with the following rewrite rules:
RewriteEngine On
RewriteBase /

# If the request is for a real file or directory, don't rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all requests to index.php
RewriteRule ^(.*)$ index.php [QSA,L]
If your application is in a subdirectory (e.g., /apitest), adjust the RewriteBase:
RewriteEngine On
RewriteBase /apitest

# If the request is for a real file or directory, don't rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all requests to index.php
RewriteRule ^(.*)$ index.php [QSA,L]
4

Restart Apache

After making configuration changes, restart Apache:
sudo systemctl restart apache2
5

Test Configuration

Test your FastRoute setup by accessing a route:
curl http://localhost/api/test
If configured correctly, the request should be handled by your FastRoute dispatcher in index.php.

Understanding the Rewrite Rules

The .htaccess configuration contains three key components:

RewriteEngine On

Enables the Apache rewrite engine for URL manipulation.

RewriteCond Directives

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These conditions ensure that:
  • !-f: Requests for actual files (images, CSS, JS) are served directly
  • !-d: Requests for actual directories are not rewritten
  • Only non-existent paths are routed through index.php

RewriteRule

RewriteRule ^(.*)$ index.php [QSA,L]
  • ^(.*)$: Matches all URLs
  • index.php: Routes matched URLs to the FastRoute dispatcher
  • QSA: Query String Append - preserves GET parameters
  • L: Last rule - stops processing further rules

Common Issues

.htaccess Not Working

If your .htaccess rules aren’t working:
  1. Verify mod_rewrite is enabled: apache2ctl -M | grep rewrite
  2. Check AllowOverride All is set in your virtual host configuration
  3. Ensure .htaccess file permissions are readable: chmod 644 .htaccess

404 Errors on Routes

If all routes return 404:
  1. Verify the RewriteBase matches your application path
  2. Check Apache error logs: tail -f /var/log/apache2/error.log
  3. Ensure index.php exists in the document root

Permission Denied Errors

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

Alternative: Virtual Host Configuration

Instead of using .htaccess, you can add rewrite rules directly to your virtual host configuration for better performance:
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted

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

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
When using virtual host configuration, set AllowOverride None and restart Apache after changes: sudo systemctl restart apache2