BlackBits.io

Laravel SEO Rewrite

Laravel SEO Rewrite

Introduction

Today we want to introduce you to a new package we recently created. The package is called Laravel SEO Rewrite. Can you guess what its used for? Let's consider you have a Post Model, and you are going to write a post that has "My Amazing Blog Post" as a title. Now you have three options for dealing with that:

  1. www.example.com/posts/1
  2. www.example.com/posts/My%20Amazing%20Blogpost
  3. www.example.com/posts/my-amazing-blogpost

The third option is the way to go here. It's clearly more readable than the two other options, and it's also good for SEO. But what would happen if you have ranked this page on Google and you change the title (slug) of the post, and it isn't accessible through www.example.com/posts/my-amazing-blogpost, but instead through www.example.com/posts/my-super-blogpost?

If somebody clicks on the first link, they would get a "404 Page Not Found", which indicates to Google that this page doesn't exist anymore, and it gets removed from the index. But, that is not what you want! You want to keep the ranking and have a 301 Moved Permanently redirect to the new page. That is where our magic starts!

Installation

The installation of the package requires just two simple steps.

1.) Require package using composer

composer require black-bits/laravel-seo-rewrite

2.) Execute Migrations

php artisan migrate

Usage

In our package, we have a Service Provider class that automatically registers the middleware. The middleware checks before every request, to see if a given path is already registered as source, and redirects the user to the defined destination, if one exists.

To create a new redirect, simply create a new SeoRewrite entry:

The source value must be a relative path matching one of your routes. The destination can be any relative or absolute URL. The type must be a valid redirect type (permanent, temporary, etc.)

SeoRewrite::create([
    'source'      => '/old-route',
    'destination' => '/new-route',
    'type'        => 301
]);

SeoRewrite::create([
    'source'      => '/old-route',
    'destination' => 'https://your-new.domain/old-route',
    'type'        => 308
]);

We run a basic redirect loop detection on model save, but not all cases can be detected. Please be aware that you can potentially create loops.

Conclusion

With our package, you can easily manage rewrites, and don't have to worry about it again. We also have detection of rewrite loops, so if a loop occurs, you would get an exception.

If you have any ideas, feedback for improvement, please drop us a message or create an issue on our Github repository, so we can take a look at that. Also feel free to contribute.

https://github.com/black-bits/laravel-seo-rewrite

Expert Level Laravel Web Development & Consulting Agency

We love Laravel, and so should you. Let us show you why.

About Us

Founded in 2014, Black Bits helps customers to reach their goals and to expand their market positions. We work with a wide range of companies, from startups that have a vision and first funding to get to market faster, to big industry leaders that want to profit from modern technologies. If you want to start on your project without building an internal dev-team first, or if you need extra expertise or resources, Black Bits – the Laravel Web Development Agency is here to help.

Laravel

Laravel

Vue.js

Vue.js

React

React

Next.js

Next.js

TailwindCSS

TailwindCSS

AWS

AWS

Laravel Cloud

Laravel Cloud

Vercel

Vercel

DigitalOcean

DigitalOcean

Cloudflare

Cloudflare

Terraform

Terraform

Kubernetes

Kubernetes

Wiz

Wiz

OpenAI

OpenAI

Stripe

Stripe

Let's Talk

You have the vision, we have the development expertise. We would love to hear about your project.

Platform down right now?

Call us directly at (+1) 541 237-0201. We're here to help!

Or send us a message. We'll get back to you within 24 hours.

Grants Pass, Oregon, U.S.A.

(+1) 541 237-0201

hello@blackbits.io