Installation Add the package via Composer:
composer require symfony/apache-pack
This installs Apache-specific tools and configurations for Symfony projects.
First Use Case: .htaccess Generation
Run the following command to generate a basic .htaccess file in your project root:
php bin/console apache:htaccess:generate
This creates a production-ready .htaccess for URL rewriting and caching.
Where to Look First
php bin/console list apache
config/packages/apache.yaml (if generated) for Apache-specific settings.URL Rewriting
Use the generated .htaccess for clean URLs (e.g., /blog/post instead of ?route=blog_post).
Example .htaccess snippet:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Caching Headers
Configure caching via .htaccess for static assets:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
</IfModule>
Environment-Specific Configs
Use the apache:htaccess:generate command with --env to generate environment-specific .htaccess files:
php bin/console apache:htaccess:generate --env=prod
Integration with Symfony’s Router
Ensure Apache’s mod_rewrite is enabled and aligned with Symfony’s routing (e.g., _routes.yaml or annotations).
Custom Directives
Extend .htaccess with custom Apache directives (e.g., security headers):
<IfModule mod_headers.c>
Header set X-Content-Type-Options "nosniff"
</IfModule>
Missing mod_rewrite
If URLs don’t rewrite, ensure Apache’s mod_rewrite is enabled:
sudo a2enmod rewrite # Debian/Ubuntu
sudo systemctl restart apache2
Overwriting .htaccess
Avoid manually editing the generated .htaccess—use the apache:htaccess:generate command to regenerate it with updates.
Case Sensitivity
Apache on Linux is case-sensitive by default. Ensure your routes and .htaccess rules account for this (e.g., RewriteRule ^Blog vs. ^blog).
Permissions
Ensure .htaccess is readable by the web server user (e.g., www-data):
chown www-data:www-data .htaccess
chmod 644 .htaccess
/var/log/apache2/error.log (or equivalent) for .htaccess parsing errors.curl -I http://localhost to verify headers (e.g., caching, security)..htaccess changes with:
apache2ctl configtest
Use AllowOverride
In your Apache virtual host config, ensure .htaccess overrides are allowed:
<Directory /var/www/project>
AllowOverride All
Require all granted
</Directory>
Combine with Other Packs
Pair with symfony/web-server-bundle for local development (e.g., php bin/console server:run).
Custom Templates
Override the default .htaccess template by copying vendor/symfony/apache-pack/Resources/stubs/.htaccess.stub to config/stubs/.htaccess.stub and customize it.
Performance
For production, combine .htaccess rules with mod_cache or mod_pagespeed for better performance.
CI/CD Integration
Automate .htaccess generation in your deployment pipeline:
# Example GitHub Actions step
- run: php bin/console apache:htaccess:generate --env=prod
How can I help you explore Laravel packages today?