Overview
FastRoute requires proper Apache configuration to enable URL rewriting. This ensures that all requests are routed throughindex.php for proper route handling.
Prerequisites
- Apache web server installed
mod_rewritemodule enabled- Access to Apache configuration files
Configuration Steps
Configure AllowOverride
Edit your Apache virtual host configuration file to enable Add or modify the
.htaccess overrides.Open the configuration file (typically located at /etc/apache2/sites-available/000-default.conf):Directory directive to enable AllowOverride:Create .htaccess File
Create a If your application is in a subdirectory (e.g.,
.htaccess file in your project root directory with the following rewrite rules:/apitest), adjust the RewriteBase:Understanding the Rewrite Rules
The.htaccess configuration contains three key components:
RewriteEngine On
Enables the Apache rewrite engine for URL manipulation.RewriteCond Directives
!-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
^(.*)$: Matches all URLsindex.php: Routes matched URLs to the FastRoute dispatcherQSA: Query String Append - preserves GET parametersL: Last rule - stops processing further rules
Common Issues
.htaccess Not Working
If your.htaccess rules aren’t working:
- Verify
mod_rewriteis enabled:apache2ctl -M | grep rewrite - Check
AllowOverride Allis set in your virtual host configuration - Ensure
.htaccessfile permissions are readable:chmod 644 .htaccess
404 Errors on Routes
If all routes return 404:- Verify the
RewriteBasematches your application path - Check Apache error logs:
tail -f /var/log/apache2/error.log - Ensure
index.phpexists in the document root
Permission Denied Errors
Alternative: Virtual Host Configuration
Instead of using.htaccess, you can add rewrite rules directly to your virtual host configuration for better performance: