Ad

Implement A Long List Of Redirections In Laravel

- 1 answer

I'm working on this Laravel project, ex : https://project.com and I have an Excel document containing more than 1000 lines for redirections that I have to add, as follow:

1- https://project.com/content -> https://project.com/new-content
2- https://project.com/news -> https://project.com/weather
3- https://project.com/pages/* -> https://project.com/
... etc

I need to redirect (redirection 301) each link on the left to the new link on the right. So I'm wondering if I need to just add all the data in an array, as key/value, and loop through it with an if statement for each entry. So everytime the user tries to access a link, he'll go through that loop

Is this the right thing to do? or there's a better/simple way to do it?

Ad

Answer

Many ways to do it but reading from excel must be last way. Because excel has encoding and it takes time.

Read it from excel and put it to the database or Redis (Create a console command [https://laravel.com/docs/5.8/artisan#registering-commands] to do it one time). You can use it at middleware to check does URL have a redirect.

If you want it as a file try text-based files not excel. Also, you can use as config file as an array and cache it with php artisan config:cache.

Don't use if-else statements, use in an array or SQL query.

But the best option is with SQL query.

Ad
source: stackoverflow.com
Ad