Check urls against unauthorized 404 page

This doesn’t work with a snippet. It has to be a plugin. (The plugins are located in the same “Elements” tab below the snippets.)

Then in the plugin code, check the value of the request parameter q ($_REQUEST['q']).
If the parameter has the format gallery/photo-x, use the function $modx->sendForward() to load the gallery page (and maybe also set $_REQUEST/$_GET['some_parameter_name'] to the value of “photo-x”, so that value can then be used on the gallery page to load the correct image).


Some sample plugin code:

<?php
$gallery_resource_id = 72; // change this to the correct ID of your 'gallery' page
$request_param_img_id = 'photo_id';

if ($modx->event->name == 'OnPageNotFound') {
	$requestParamAlias = $modx->getOption('request_param_alias', null, 'q');
	if (!isset($_REQUEST[$requestParamAlias])) {
		return;
	}	
	$q = $_REQUEST[$requestParamAlias];
	if (preg_match('#^gallery/photo-(\d+)/?$#', $q, $matches)) {
		$_GET[$request_param_img_id] = $matches[1];
		$_REQUEST[$request_param_img_id] = $matches[1];
		$modx->sendForward($gallery_resource_id);
	}
}

In the plugin in the tab “System Events”, check the checkbox for the event “OnPageNotFound”.

1 Like