We have a bunch of historical event pages that have no value, but we still have upcoming events in the events folder that we need, as well as some historical events with videos that do have value that we need to keep working.
What I want to do is redirect all the old events, without affecting the new ones. The catch: I want to delete the old ones, so I can’t use their template as a mechanism for the redirect.
My old solution: Set the old ones as invisible, then in the template check for visibility. If invisible, redirect to the main events page.
The problem: Now that I’m implementing the control panel, this makes for a VERY cluttered events section in the control panel.
What I need: To be able to use htaccess or something to redirect all subpages under events, but only if they no longer physically exist.
I could have the 404 template do the redirect for me, but that kind of seems like bad form.
Anybody know enough about htaccess to pull something like this off?
You could even pass a message from the route to the template if the user requested an invalid page, something along the lines: “The event you requested does no longer exist. Maybe you are interested in one of our upcoming events…”
@texnixe related: instead of “visit” could I do “redirect” or something? Right now it keeps the old URL, which I guess is OK, but ideally we want Google to realize it’s redirected, and we don’t want the end user to get confused as to why that URL is showing the full events list.
Edit: Oh, duh, use go() instead of site()->visit().
This is fine if you want to redirect individual URLs to different targets, but for the above use case keeping a redirect list is not necessary at all. Just out of curiosity, how would the hook know where to redirect to unless you specify this manually somewhere (where, in the page you are about to delete?).
In my use it was a list of ~40 pages so the client did it manually for the URLs the didn’t have a match on the new website. What I suggested using the page delete hook would only work on Matthew’s use case, since all deleted pages would redirect to the same page.
In cases where I have a fixed list of pages that should redirect somewhere else, I usually do this in the .htaccess or ideally in the server configuration. This is preferable to routes, because PHP doesn’t have to kick in first.
In the current case, there is probably no pattern nor a fixed list that you could use, so I think a route is the only way to achieve this.
Using the page delete hook instead of htaccess could make sense in some situations, just because it would be automatic instead of requiring the user to go manually enter something in htaccess after deleting it. However, I’m not entirely sure where that would be advantageous as opposed to just using the generic route method above.
The only use case for this that I can think of would be if you redirect those deleted pages to individual targets that you would save in the page before you delete it, so that the delete hook could fetch that information and add it to the redirect list. You wouldn’t be able to do this with a generic route. But I don’t know if that would be a a feasible workflow.
You post an article and then realize there’s an embarrassing typo in the URL slug. You don’t want a generic redirect because you want it to link to the correct version of the page, and you don’t want someone to have to dig into .htaccess to fix it.
So, you create a new one, update a redirect field or something in the old one, and then when you delete the old one, have it add it to the redirect list with the contents of the redirect field.
This actually would be cool if it was built into Kirby where you could change the slug of a page and have it automagically redirect the old version.