Automatic Redirect from Detail Page to List Page or “Error 404”
The data output on the detail page is often controlled or filtered via one or more parameters —
usually via the auto_item.
If no record can be found due to the filter, or if the detail page is accessed without providing the (filter) parameter in the URL, a message such as “No data could be found” is displayed.
If this is not desired and the user should instead be redirected directly to the list view, this can be achieved with the following code in the detail view template:
1 // redirect if data empty
2 if (!count($this->data)) {
3 $pageId = 192; // Page id
4 $page = \PageModel::findByPK($pageId);
5 $pageURL = $page->getFrontendUrl();
6 \Controller::redirect($pageURL);
7 }
If the Contao base page is accessed without providing the (filter) parameter, an “Error 404” can also be delivered automatically. To do this, enable the “Requires item” checkbox in the page settings.