Trying to find a simple way to cloak/redirect a larger amount of URLs as doing it manually would be quite tedious. I have tried Retour, but it would be very time-consuming to manually set up all the redirects and with the correct language translations as well. I currently have a pages field, and within each page, there is an external_url link field. I tried to set up a redirect.php template and use go(), but I can’t find a way to pass in the external_url variable from another template. Any ideas?
The pages field lists reviews for external products. For each review I would like to have a “Go to Source” button that would be cloaked/redirected to an external URL and then a button that would say “See Our Review” which would go to the page itself. Is this setup possible?
Sorry, I should’ve expanded on that a bit more. The external URL would be cloaked so instead of the full external link address it would be something like domain.com/redirect/review-name. Which would then redirect to the external link.
I already have the review pages set up, with a field for the external URL, would it be possible to set up the redirect to just use this field? Or would I have to create a separate pages module for the redirects?
Great, the page model is what I was looking for… for the routing, would it be possible to pull in the field from the panel? So it would look something like this:
Yes, you would try to get the page from your pattern
'routes' => [
[
'pattern' => 'redirect/(:all)',
'action' => function ($uri) {
// find the review page
if ($page = page($uri) ) {
go($page->externalUrl());
}
}
]
]
This model doesn’t make sense, the name is not correct and you need a model for your Review page, that doesn’t return the external URL but the cloaked URL.
class ReviewPage extends Page {
public function cloakedUrl() {
return url('redirect/' . $this->id());
}
}
This model return the URL for the route pattern, you use it for your external button.
The route then does the redirect based on the content of the externalUrl field.
Ah figured it out, was simply an issue with the name of the extended page. I had it as ReviewsPage, when it should’ve been ReviewPage. Redirect working as intended now Thanks for all your help guys. Quick question, will it work okay with translations as well?
Would there be a way to add access to the regular page with this solution? For example, one link would be “Go to Product” which would use the redirect (domain.com/redirect/product-name), and the other would be a “See our Review” which would go to the corresponding child page (domain.com/reviews/product-name). Something like the example below, I’m just not sure how to check for which button is clicked.