TL;DR

No time? No problem... We provide expert level Laravel development and consulting. We also help customers scaling their projects. If you think we could fit together, drop us a message or give us a call. We are here to help.

Contact
Grants Pass, Oregon, U.S.A.
phone:
(+1) 541 592 9181

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

1
2composer require black-bits/laravel-seo-rewrite
3

2.) Execute Migrations

1
2php artisan migrate
3

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.)

1SeoRewrite::create([
2    'source'      => '/old-route',
3    'destination' => '/new-route',
4    'type'        => 301
5]);
6
7SeoRewrite::create([
8    'source'      => '/old-route',
9    'destination' => 'https://your-new.domain/old-route',
10    'type'        => 308
11]);

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

View all articles

Expert Level Laravel Web Development & Consulting Agency

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

Find out about Laravel

About Us

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 is one of the most popular PHP Frameworks. Perfect for Sites and API's of all sizes. Clean, fast and reliable.

Lumen

Lumen is the perfect solution for building Laravel based micro-services and blazing fast APIs.

Vue.js

Live-Updating Dashboards, Components and Reactivity. Your project needs a modern user-interface? Vue.js is the right choice.

React

Live-Updating Dashboards, Components and Reactivity. Your project needs a modern user-interface? React is the right choice.

Bootstrap

Bootstrap is the second most-starred project on GitHub and the leading framework for designing modern front-ends.

TailwindCSS

Tailwind is a utility-first CSS framework for rapidly building custom user interfaces.

Amazon Web Services

Amazon Web Services (AWS) leads the worldwide cloud computing market. With a wide range of services, AWS is a perfect option for fast development.

Digital Ocean

Leave complex pricing structures behind. Always know what you'll pay per month. Service customers around the world from eight different locations.

Contact Us

Contact Us

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

Send us a message or give us a call and see how Black Bits can help you!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Mountain view.