Installation:
composer require wozbe/redirect-bundle
Add to AppKernel.php:
new Wozbe\RedirectBundle\WozbeRedirectBundle(),
Basic Configuration (config/packages/wozbe_redirect.yaml):
wozbe_redirect:
domains:
example.com:
aliases:
- www.example.com
- example.com.old
First Use Case:
AppKernel.php to ensure redirects fire early.http://www.example.com—it should redirect to http://example.com.Define Domains:
Configure all domain variants in config/packages/wozbe_redirect.yaml under domains. Use a canonical domain (e.g., example.com) and list all aliases (e.g., www.example.com).
Event-Based Integration:
kernel.request event. No manual checks needed in controllers.wozbe_redirect.pre_redirect (Symfony 2.x) or extend the bundle’s RedirectListener.Dynamic Configuration:
Wozbe\RedirectBundle\Domain\DomainManager and inject your data source.# config/services.yaml
Wozbe\RedirectBundle\Domain\DomainManager:
arguments:
$domains: '%kernel.project_dir%/config/domains.json'
Path Preservation:
/blog → /blog on the canonical domain).preserve_path: false in config.HTTP Status Codes:
301 (permanent). Change globally:
wozbe_redirect:
status_code: 302
Order Matters:
WozbeRedirectBundle before other bundles (e.g., FrameworkBundle) in AppKernel.php. Otherwise, redirects may fail silently.Case Sensitivity:
WWW.EXAMPLE.COM ≠ www.example.com).HTTPS/HTTP Mismatches:
http → https). Use Symfony’s HttpsRedirectListener or a reverse proxy (e.g., Nginx) for this.Subdomains vs. Domains:
sub.example.com as a separate domain. For wildcard redirects, use a regex alias:
example.com:
aliases:
- '*.example.com' # Redirects all subdomains to example.com
Caching Headers:
Cache-Control headers don’t interfere with redirects. Test with curl -I.Check Redirects:
APP_DEBUG=true) to see if the bundle fires. Look for 301/302 logs in Symfony’s profiler.Log Configuration:
config/packages/monolog.yaml to log redirects:
handlers:
redirect:
type: stream
path: "%kernel.logs_dir%/redirect.log"
level: debug
channels: ["wozbe_redirect"]
Bypass Redirects:
AppKernel.php to isolate issues:
// Disable for testing
// new Wozbe\RedirectBundle\WozbeRedirectBundle(),
Custom Redirect Logic:
RedirectListener to add conditions (e.g., skip redirects for /admin):
// src/EventListener/CustomRedirectListener.php
use Wozbe\RedirectBundle\Event\PreRedirectEvent;
public function onPreRedirect(PreRedirectEvent $event) {
if ($event->getRequest()->getPathInfo() === '/admin') {
$event->setRedirect(false);
}
}
Register as a service with tag kernel.event_listener and priority 255 (highest).Database-Backed Domains:
DomainManager to fetch domains from Doctrine:
use Doctrine\ORM\EntityManagerInterface;
public function __construct(EntityManagerInterface $em) {
$domains = $em->getRepository(Domain::class)->findAll();
parent::__construct($this->mapDomains($domains));
}
Internationalized Domain Names (IDN):
wozbe_redirect:
convert_idn: true
Analytics Tracking:
wozbe_redirect.post_redirect event to log redirects to Google Analytics:
public function onPostRedirect(PostRedirectEvent $event) {
// Track via GA or custom analytics
}
How can I help you explore Laravel packages today?