Laravel package for manage your URL redirects in database or other sources to get better SEO results

Overview

Laravel package for manage your URL redirects in database or other sources to get better SEO results

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Requirements

You need PHP ^7.4 or higher. It is only tested and was designed for Laravel 8 and 9. This package will receive updates for future Laravel versions. Previous Laravel versions are not contemplated so use Neurony/laravel-redirects package for older Laravel versions.

Installation

You can install the package via composer:

composer require SiroDiaz/laravel-redirection

You can publish and run the migrations with:

php artisan vendor:publish --provider="SiroDiaz\Redirection\RedirectionServiceProvider" --tag="redirection-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="SiroDiaz\Redirection\RedirectionServiceProvider" --tag="redirection-config"

This is the contents of the published config file:

[ 301 => 'Permanent (301)', 302 => 'Normal (302)', 307 => 'Temporary (307)', ], /* |-------------------------------------------------------------------------- | Default Redirect status code (in case of not defined) |-------------------------------------------------------------------------- | | Status code used by default to redirect from an old URL to a new mapped | URL. | */ 'default_status_code' => (int)env('REDIRECT_DEFAULT_STATUS', 301), /* |-------------------------------------------------------------------------- | Case sensitivity |-------------------------------------------------------------------------- | | Whether to match URLs case sensitively or not. | Default to false because most URLs are not case sensitive. | */ 'case-sensitive' => (bool) env('REDIRECT_CASE_SENSITIVE', false), /* |-------------------------------------------------------------------------- | Redirect Driver |-------------------------------------------------------------------------- | | Here you may specify the default redirect driver that you want to use. | The "config" driver is used by default when you want to code faster. | Consider database driver better for admin panel configuration backed by | a relational DB. | */ 'driver' => env('REDIRECT_DRIVER', 'config'), /* |-------------------------------------------------------------------------- | Array containing all available drivers and its implementations and source |-------------------------------------------------------------------------- | | Concrete implementation for the "redirection model". | To extend or replace this functionality, change the value below with | your full "redirection model" FQN. | | Your class will have to (first option is recommended): | - extend the "SiroDiaz\Redirection\Models\Redirection" class | - or at least implement the "SiroDiaz\Redirection\Contracts\RedirectionModelContract" interface. | | Regardless of the concrete implementation below, you can still use it like: | - app('redirection.') OR app('\SiroDiaz\Redirection\Contracts\RedirectionModelContract') | - or you could even use your own class as a direct implementation. For this | case you must extend from "SiroDiaz\Redirection\Models\Redirection" model class and | replace in the published config file 'drivers.database.source'. | | */ 'drivers' => [ 'config' => [ 'driver' => SiroDiaz\Redirection\Drivers\FileRedirector::class, 'source' => 'redirection.urls', ], 'database' => [ 'driver' => SiroDiaz\Redirection\Drivers\DatabaseRedirector::class, 'source' => SiroDiaz\Redirection\Models\Redirection::class, ], ], /* |-------------------------------------------------------------------------- | Url list with redirections used for config driver |-------------------------------------------------------------------------- | | You can use urls array of two different ways. The simple one uses the | default redirect status code ('redirection.default_status_code'). | Example: | 'urls' => [ | '/old/url' => '/new/url', | '/another/old/url' => '/another/new/url', | '/url/with?id=123' => '/url/with/123', | ], | | The second way to write redirect urls in your config/redirection.php | is using associative arrays. You can combine this method with the previous one. | Look at this example: | 'urls' => [ | '/old/url' => ['new_url' => '/new/url', 'status_code' => 302], | '/another/old/url' => '/another/new/url', | '/url/with?id=123' => ['new_url' => '/url/with/123'], | ], | */ 'urls' => [], ]; ">


return [
    /*
    |--------------------------------------------------------------------------
    | Status codes valid for redirections
    |--------------------------------------------------------------------------
    |
    | The redirect statuses that you will use in your application.
    | By default, the "301", "302" and "307" are defined.
    |
    */
    'statuses' => [
        301 => 'Permanent (301)',
        302 => 'Normal (302)',
        307 => 'Temporary (307)',
    ],

    /*
    |--------------------------------------------------------------------------
    | Default Redirect status code (in case of not defined)
    |--------------------------------------------------------------------------
    |
    | Status code used by default to redirect from an old URL to a new mapped
    | URL.
    |
    */
    'default_status_code' => (int)env('REDIRECT_DEFAULT_STATUS', 301),

    /*
    |--------------------------------------------------------------------------
    | Case sensitivity
    |--------------------------------------------------------------------------
    |
    | Whether to match URLs case sensitively or not.
    | Default to false because most URLs are not case sensitive.
    |
    */
    'case-sensitive' => (bool) env('REDIRECT_CASE_SENSITIVE', false),

    /*
    |--------------------------------------------------------------------------
    | Redirect Driver
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default redirect driver that you want to use.
    | The "config" driver is used by default when you want to code faster.
    | Consider database driver better for admin panel configuration backed by
    | a relational DB.
    |
    */
    'driver' => env('REDIRECT_DRIVER', 'config'),

    /*
    |--------------------------------------------------------------------------
    | Array containing all available drivers and its implementations and source
    |--------------------------------------------------------------------------
    |
    | Concrete implementation for the "redirection model".
    | To extend or replace this functionality, change the value below with
    | your full "redirection model" FQN.
    |
    | Your class will have to (first option is recommended):
    | - extend the "SiroDiaz\Redirection\Models\Redirection" class
    | - or at least implement the "SiroDiaz\Redirection\Contracts\RedirectionModelContract" interface.
    |
    | Regardless of the concrete implementation below, you can still use it like:
    | - app('redirection.') OR app('\SiroDiaz\Redirection\Contracts\RedirectionModelContract')
    | - or you could even use your own class as a direct implementation. For this
    | case you must extend from "SiroDiaz\Redirection\Models\Redirection" model class and
    | replace in the published config file 'drivers.database.source'.
    |
    |
    */
    'drivers' => [
        'config' => [
            'driver' => SiroDiaz\Redirection\Drivers\FileRedirector::class,
            'source' => 'redirection.urls',
        ],
        'database' => [
            'driver' => SiroDiaz\Redirection\Drivers\DatabaseRedirector::class,
            'source' => SiroDiaz\Redirection\Models\Redirection::class,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Url list with redirections used for config driver
    |--------------------------------------------------------------------------
    |
    | You can use urls array of two different ways. The simple one uses the
    | default redirect status code ('redirection.default_status_code').
    | Example:
    | 'urls' => [
    |   '/old/url' => '/new/url',
    |   '/another/old/url' => '/another/new/url',
    |   '/url/with?id=123' => '/url/with/123',
    | ],
    |
    | The second way to write redirect urls in your config/redirection.php
    | is using associative arrays. You can combine this method with the previous one.
    | Look at this example:
    | 'urls' => [
    |   '/old/url' => ['new_url' => '/new/url', 'status_code' => 302],
    |   '/another/old/url' => '/another/new/url',
    |   '/url/with?id=123' => ['new_url' => '/url/with/123'],
    | ],
    |
    */
    'urls' => [],

];

You can change and extend the default SiroDiaz\Redirection\Models\Redirection model class. Image that you want to add some methods or fields. You would need to create a new model class, for example App\Models\Redirect.

Here is a basic example of how to extend the functionality of the default Redirection model. We want to include support for Laravel BackPack admin panel would be:



namespace App\Models;

use SiroDiaz\Redirection\Models\Redirection as RedirectionBaseModel;
use Backpack\CRUD\app\Models\Traits\CrudTrait;

class Redirect extends Redirection
{
    use CrudTrait;
}

Finally, you have to vendor:publish the sirodiaz/laravel-redirection config file to change the model class used now.

If you want to add more status codes for another type of redirect purposes, add them to your config/redirection.php config file. By default, you have the most common redirections codes: 301, 302 and 307.

Ey! don't forget to append the middleware SiroDiaz\Redirection\RedirectRequests to your app/Http/Kernel.php file

// app/Http/Kernel.php

    protected $middleware = [
        ...
        \SiroDiaz\Redirection\RedirectRequests::class,
    ],

Extending and creating new redirect Drivers

TODO

Testing this package for contribution

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

You might also like...
A package to handle the SEO in any Laravel application, big or small.
A package to handle the SEO in any Laravel application, big or small.

Never worry about SEO in Laravel again! Currently there aren't that many SEO-packages for Laravel and the available ones are quite complex to set up a

A convenient helper for using the laravel-seo package with Filament Admin and Forms
A convenient helper for using the laravel-seo package with Filament Admin and Forms

Combine the power of Laravel SEO and Filament PHP. This package is a convenient helper for using the laravel-seo package with Filament Admin and Forms

Easily setup SEO in your laravel project with lara-head :heart: @code4mk
Easily setup SEO in your laravel project with lara-head :heart: @code4mk

installation composer require code4mk/lara-head usage meta ~ inside controller use Khead; class Test { public function home() { Khead::setMeta

Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

SEO Tools for Laravel
SEO Tools for Laravel

SEOTools - SEO Tools for Laravel and Lumen SEOTools is a package for Laravel 5.8+ and Lumen that provides helpers for some common SEO techniques. Curr

This is an open source demo of administration panel for polymorphic relationship and SEO content

Laravel SEO admin This application demonstrates usage of polymorphic relationships described at (http://maxoffsky.com/code-blog/using-polymorphic-rela

Laravel-OvalFi helps you Set up, test, and manage your OvalFi integration directly in your Laravel App.
Laravel-OvalFi helps you Set up, test, and manage your OvalFi integration directly in your Laravel App.

OvalFi Laravel Package Laravel-OvalFi helps you Set up, test, and manage your OvalFi integration directly in your Laravel App. Installation You can in

Laravel Proxy Package for handling sessions when behind load balancers or other intermediaries.

Laravel Trusted Proxies Setting a trusted proxy allows for correct URL generation, redirecting, session handling and logging in Laravel when behind a

A package for Myanmar Font, Phone and other Myanmar tools using Laravel Macro

Laravel Myanmar Tools A package for Myanmar Font, Phone and other Myanmar tools using Laravel Macro. Installation composer require pyaesoneaung/larave

Comments
Owner
Siro Díaz Palazón
Entrepreneur, Web and mobile dev. Always learning new technologies, marketing and more.
Siro Díaz Palazón
Laravel SEO - This is a simple and extensible package for improving SEO via meta tags, such as OpenGraph tags.

This is a simple and extensible package for improving SEO via meta tags, such as OpenGraph tags.

ARCHTECH 191 Dec 30, 2022
Laravel Seo package for Content writer/admin/web master who do not know programming but want to edit/update SEO tags from dashboard

Laravel Seo Tools Laravel is becoming more and more popular and lots of web application are developing. In most of the web application there need some

Tuhin Bepari 130 Dec 23, 2022
SEO Helper is a package that provides tools and helpers for SEO (Search Engine Optimization).

SEO Helper By ARCANEDEV© SEO Helper is a package that provides tools and helpers for SEO (Search Engine Optimization). Feel free to check out the rele

ARCANEDEV 301 Nov 25, 2022
Get the thumbnail of youtube and vimeo videos from the url. The returned information is ID and URL of the thumbnail

Video Thumbnail URL Get the thumbnail of youtube and vimeo videos from the url. The returned information is ID and URL of the thumbnail Installation I

Fernando Valler 2 Jan 22, 2022
This repo is for the Laracon 2021 talk "Manage SEO with Laravel and Nova"

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Kristin 7 Dec 9, 2022
Package to optimize your site automatically which results in a 35%+ optimization

Laravel Page Speed Simple package to minify HTML output on demand which results in a 35%+ optimization. Laravel Page Speed was created by Renato Marin

Renato Marinho 2.2k Dec 28, 2022
Manage meta data based on URL path within your app.

Laravel SEO Manager This package provides simple functionality to manage SEO tags based on URL path within your Laravel application. You can put the U

Michael Rubel 20 Oct 20, 2022
A simple way to add 301/302 redirects within CraftCMS.

Redirector plugin for Craft CMS 3.x A simple way to add 301/302 redirects within CraftCMS. This is the first CraftCMS plugin written by myself so plea

Jae Toole 1 Nov 25, 2021
A program which shows the match days and results of a sport league developed with HTML, PHP and BladeOne technology

league-table Escribe un programa PHP que permita al usuario introducir los nombres de los equipos que participan un una liga de fútbol. Con dichos nom

ManuMT 1 Jan 21, 2022
Seo Manager Package for Laravel ( with Localization )

Seo Manager Package for Laravel ( with Localization ) lionix/seo-manager package will provide you an interface from where you can manage all your page

Lionix 205 Dec 23, 2022