Hi there!
I’ve updated a website which contains a thousand of pages and would like to make some 301 redirections so the old links point to the new ones.
I’ve created some routes for the main pages which work fine:
[
'pattern' => 'category/albums',
'action' => function() {
return go('albums', 301);
}
],
[
'pattern' => 'category/romans',
'action' => function() {
return go('romans', 301);
}
],
[
'pattern' => 'category/bd',
'action' => function() {
return go('bd', 301);
}
],
[
'pattern' => 'category/auteurs',
'action' => function() {
return go('auteurs', 301);
}
],
[
'pattern' => 'contact',
'action' => function() {
return go('contacts', 301);
}
],
BUT, I didn’t find a nice solution to redirect all the other pages. Those were hosted at the root of the website like:
- website.com/book-title
- website.com/author-name
Now all the pages are organized in subfolders like:
- website.com/albums/book-title
- website.com/romans/book-title
- website.com/bd/book-title
- website.com/auteurs/author-name
90% of the page slugs are the same between the old and new website… so my idea was to create a function that check if a new page exists with an old slug and redirect either to this page, or the homepage.
But I could not find a proper way to do this…
Here is what I have tried without success:
[
other routes listed above
],
[
'pattern' => '(:any)',
'action' => function($id) {
$page = pages()->findBy('slug', $id);
return go($page, 301);
}
]
Any idea what I am doing wrong?
Thank you.