Download the PHP package vormkracht10/laravel-redirects without Composer
On this page you can find all versions of the php package vormkracht10/laravel-redirects. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vormkracht10/laravel-redirects
More information about vormkracht10/laravel-redirects
Files in vormkracht10/laravel-redirects
Package laravel-redirects
Short Description Add redirects to your Laravel app using a database, so you can dynamically manage it without writing code.
License MIT
Homepage https://github.com/backstagephp/laravel-redirects
Informations about the package laravel-redirects
Laravel Redirects
A powerful and flexible Laravel package for managing HTTP redirects through a database-driven approach. Dynamically create, update, and track redirects without modifying code or redeploying your application.
Features
- Database-driven redirects - Manage redirects dynamically without code changes
- Multiple matching strategies - HTTP, wildcard, and strict matching middleware
- Status code support - 301, 302, 307, 308 redirects
- Query string preservation - Automatically maintains query parameters
- Hit tracking - Monitor redirect usage with built-in analytics
- Event-driven automation - Automatically create redirects when URLs change
- SEO-friendly - Proper HTTP status codes and trailing slash handling
- Case sensitivity control - Configure case-sensitive or insensitive matching
- Trailing slash handling - Flexible trailing slash sensitivity options
- Protocol agnostic - Works with HTTP and HTTPS seamlessly
Table of Contents
- Installation
- Configuration
- Usage
- Creating Redirects
- Automatic Redirects via Events
- Query String Handling
- Tracking Redirect Hits
- Middleware
- HttpRedirects
- WildRedirects
- StrictRedirects
- Advanced Usage
- Custom Redirect Model
- Managing Redirects Programmatically
- API Reference
- Testing
- Changelog
- Contributing
- Security
- Credits
- License
Installation
You can install the package via Composer:
Publish and run the migrations:
This will create a redirects table with the following structure:
| Column | Type | Description |
|---|---|---|
| ulid | ULID | Primary key |
| source | string | The source URL to redirect from |
| destination | string | The destination URL to redirect to |
| code | integer | HTTP status code (301, 302, 307, 308) |
| hits | integer | Number of times this redirect was triggered |
| created_at | timestamp | When the redirect was created |
| updated_at | timestamp | When the redirect was last modified |
Optionally, publish the configuration file:
Configuration
The configuration file config/redirects.php provides extensive customization options:
Environment Variables
Add these to your .env file for environment-specific configuration:
Usage
Creating Redirects
Database Seeder
Programmatically
Via Tinker
Automatic Redirects via Events
The package includes an event-listener system to automatically create redirects when URLs change. This is useful when updating slugs or moving content:
This automatically creates a redirect in the database:
Integration Example with Eloquent Models:
Query String Handling
The package automatically preserves query strings from the source URL and appends them to the destination:
When a user visits:
They are redirected to:
If the destination already has query parameters:
Visiting /old-page?baz=qux redirects to:
Tracking Redirect Hits
Every time a redirect is triggered, the hits counter increments automatically:
Use this data for analytics and monitoring:
Middleware
The package includes three middleware classes, each with different matching strategies. They run in the order defined in config/redirects.php.
HttpRedirects
Matches URLs with protocol and www variations normalized:
This matches all of these URLs:
http://example.com/pagehttps://example.com/pagehttp://www.example.com/pagehttps://www.example.com/page
WildRedirects
Performs partial URL matching using contains():
This matches:
/blog/post-1→/articles//blog/category/tech→/articles//old-blog/archive→/articles/
StrictRedirects
Exact URL matching without query strings:
This only matches:
http://example.com/exact-pagehttps://example.com/exact-pagehttp://www.example.com/exact-page
But NOT:
example.com/exact-page/sub-pageexample.com/other-exact-page
Customizing Middleware Order
The middleware runs in the order defined in your config. First match wins:
You can reorder or remove middleware as needed. For example, to only use exact matching:
Advanced Usage
Custom Redirect Model
Create your own model extending the base Redirect model:
Update your config:
Managing Redirects Programmatically
Bulk Creation:
Import from CSV:
Conditional Redirects:
Redirect Chains (avoid these):
API Reference
Redirect Model
Properties:
ulid(string) - Primary keysource(string) - Source URLdestination(string) - Destination URLcode(int) - HTTP status codehits(int) - Number of redirects performedcreated_at(timestamp)updated_at(timestamp)
Methods:
Events
UrlHasChanged Event:
Properties:
oldUrl(string) - The old URLnewUrl(string) - The new URLcode(int) - HTTP status code (default: 301)
Listeners
RedirectOldUrlToNewUrl Listener:
Automatically creates a redirect when UrlHasChanged event is dispatched.
HTTP Status Codes
Understanding when to use each status code:
| Code | Name | Use Case | Cached by Browsers |
|---|---|---|---|
| 301 | Moved Permanently | Permanent content relocation, old URL will never be used again | Yes |
| 302 | Found | Temporary redirect, old URL may be used again | No |
| 307 | Temporary Redirect | Temporary redirect that preserves HTTP method (POST stays POST) | No |
| 308 | Permanent Redirect | Permanent redirect that preserves HTTP method (POST stays POST) | Yes |
Recommendations:
- Use 301 for most permanent redirects (blog posts, pages, renamed resources)
- Use 302 for temporary situations (maintenance pages, A/B testing)
- Use 307 when redirecting form submissions temporarily
- Use 308 when permanently moving an API endpoint that receives POST/PUT/DELETE requests
Testing
Run the test suite:
Run tests with coverage:
Run static analysis:
Fix code style:
Writing Tests:
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details on how to contribute to this package.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Security Considerations:
- Validate redirect destinations to prevent open redirects
- Sanitize user input when creating redirects programmatically
- Monitor for redirect loops and chains
- Implement rate limiting to prevent abuse
- Use HTTPS for all redirect destinations when possible
Credits
- Mark van Eijk - Creator and maintainer
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Support
Built with by Backstage CMS
All versions of laravel-redirects with dependencies
backstage/laravel-trailing-slash Version ^0.1
illuminate/contracts Version ^10.0 || ^11.0 || ^12.0||^13.0
spatie/laravel-package-tools Version ^1.16