Easy URL rewrites in your Laravel application

1.1.3 2020-01-10 12:47 UTC

This package is auto-updated.

Last update: 2021-09-10 16:44:08 UTC


Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads

Very easy to use URL rewrite package. Follow the instructions and you're good to go!

You can find an example project on my Github: view example project
This example project features the following:

  • Eloquent observers to add URL rewrites automatically
  • Usage of the trait
  • Some use cases


This package requires Laravel 5.8 or higher, PHP 7.2 or higher and a database that supports json fields and functions such as MySQL 5.7 or higher.


You can install the package via composer:

composer require ruthgeridema/laravel-url-rewrites

The package will automatically register itself.

Register the routes the feeds will be displayed on using the rewrites-macro.
You need to place it at the bottom of your routes file.

// In routes/web.php

You can publish the migration with:

php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="migrations"

After the migration has been published you can create the url_rewrites table by running the migration:

php artisan migrate

You can optionally publish the config file with:

php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="config"

This is the contents of the published config file:


return [
    'table-name' => 'url_rewrites',
    'repository' => \RuthgerIdema\UrlRewrite\Repositories\UrlRewriteRepository::class,
    'model' => \RuthgerIdema\UrlRewrite\Entities\UrlRewrite::class,
    'cache' => true,
    'cache-decorator' => \RuthgerIdema\UrlRewrite\Repositories\Decorators\CachingUrlRewriteRepository::class,
    'types' => [
        'product' => [
            'route' => 'product',
            'attributes' => ['id'],
        'category' => [
            'route' => 'category',
            'attributes' => ['id'],

Laravel Nova

Using Laravel Nova? You can publish the Nova class to App/Nova with the following command

php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="nova"

In the near future I will publish a Laravel Nova package with features like reindexing the URL rewrites.


Forward request

Let's say you've got a controller route 'product/{id}' and you have a product 'Apple Airpods' with id=5.
When you visit 'apple-airpods' this package will forward the request to the controller but keeps the clean url.

The following code adds this to the database:

UrlRewrite::create('apple-airpods', 'product/5')

Use named routes

You must specify the types in the config.

UrlRewrite::create('apple-airpods', null, 'product', ["id" => 5])

To regenerate the target path you can use


To automatically add the URL attribute to an Eloquent model, you have to add the HasUrlRewrite trait to an Eloquent model.
You also need to add the urlRewriteType and optionally add 'url' to the appends array.

use HasUrlRewrite;
public $urlRewriteType = 'category';
protected $appends = ['url'];

Once this is done you can simply call Model::find(1)->url to get the url of the model.


301 redirect

UrlRewrite::create('apple-airpods', 'product/5', null, null, 1)

302 redirect

UrlRewrite::create('apple-airpods', 'product/5', null, null, 2)

Other functions

UrlRewrite::update($data, $id)
UrlRewrite::getByTypeAndAttributes('product', ["id" => 5])


  1. Copy .env.example to .env and fill in your database credentials.
  2. Run composer test.


Please see CHANGELOG for more information what has changed recently.


Please see CONTRIBUTING for details.


If you discover any security related issues, please email instead of using the issue tracker.


Special thanks for Spatie for their guidelines and their packages as an inspiration


The MIT License (MIT). Please see License File for more information.