Implement A Long List Of Redirections In Laravel
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?
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.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?