Installation
composer require amnl/router-unslash
(Note: Due to Symfony 2.3 dependency, ensure compatibility with your Laravel project via Symfony Bridge or consider alternatives like spatie/laravel-honeypot for modern Laravel.)
Publish Config
php artisan vendor:publish --provider="AMNL\RouterUnslashBundle\RouterUnslashBundle" --tag="config"
(Edit config/amnl_router_unslash.php to adjust permanent, public, and cache settings.)
First Use Case
/about or /about/ return 404s.routes/web.php:
use AMNL\RouterUnslashBundle\RouterUnslashBundle;
// Register bundle (if using Symfony Bridge)
$app->register(new RouterUnslashBundle());
/about → /about/ (or vice versa) with configurable HTTP status (301/302).Route Normalization
RouteServiceProvider:
public function boot()
{
parent::boot();
$this->app->make('router')->getCompiler()->getRouteCollector()->getRouteCompiler()->setNormalizer(
new AMNL\RouterUnslashBundle\Normalizer\UnslashNormalizer()
);
}
/posts/1 and /posts/1/ resolve to the same handler.Conditional Activation
/api/*) via middleware:
$router->pushMiddlewareToGroup('web', \AMNL\RouterUnslashBundle\HttpFoundation\UnslashMiddleware::class);
admin/ and adding to ignored_paths in config.Cache Optimization
public: true and maxage for CDN-friendly redirects:
'amnl_router_unslash' => [
'permanent' => env('PROD') ? true : false, // 301 in production
'maxage' => 3600, // 1-hour cache for non-permanent redirects
],
app('router')->getRouteCollector()->getCompiler()->getRouteCompiler()->setNormalizer() in AppServiceProvider@boot().$this->get('/about')->assertRedirect('/about/');
permanent: true in production to pass link equity to canonical URLs.Symfony 2.3 Dependency
RouterUnslashBundle with laravel-slash (modern alternative).Overzealous Redirects
/admin vs /admin/).'ignored_paths' => [
'admin/*',
'api/*',
],
Cache Headers
public: true may expose redirects to CDNs prematurely.smaxage for shared caches and maxage for private browsers.dd() in UnslashMiddleware to trace unexpected redirects.curl -v http://example.com/about to verify 301/302 responses.Custom Normalizers
UnslashNormalizer to handle edge cases (e.g., query strings):
class CustomNormalizer extends UnslashNormalizer {
protected function normalizePath($path) {
return parent::normalizePath(rtrim($path, '?'));
}
}
Dynamic Config
config(['amnl_router_unslash.permanent' => env('APP_ENV') === 'production']);
Event Listeners
RouterUnslashEvents (if extended) to log redirects or block specific paths.spatie/laravel-honeypot to block slash-spam bots while keeping redirects clean.laravel-debugbar to audit unhandled slash variations.<link rel="canonical"> to templates to reinforce SEO consistency.How can I help you explore Laravel packages today?