Check urls against unauthorized 404 page

I have a photo gallery page in which each photo has its own individual address (custom address) url set by scripts js.

For example:
my site: exampleadomain.com/gallery
gallery:
eexampleadomain.com/gallery/photo-1
exampleadomain.com/gallery/photo-2
exampleadomain.com/gallery/photo-3

When the user enter: exampleadomain.com/gallery/photo-1
it is understandable that he will open a 404 (unauthorized page).

And now the question:
**How to set / add (custom addresses) in MODX or how to add a snippet by entering the 404 (unauthorized page). **

The only idea I have now is to add a snippet on the 404 (unauthorized page) and check the addresses and then redirect them. But this is not a good solution, because every time a user enters the 404 page (even though it does not show up, because the check takes a few milliseconds), but it will be recorded in the statistics of visits to the site.

I have a 404 page set up (System > System Settings > error_page)
I am using modx version 3.0.3

Take a look at the extra CustomRequest:

https://extras.modx.com/package/customrequest

or write your own plugin, that runs on the event OnPageNotFound.

Thank you very much for the hints. I have done so:
I added a snippet [[!check404]] at the end of the page in which :

session_start();
echo '<div data-parameter="'.$_SESSION["parameter"].'" ></div>';

if ($modx->event->name == 'OnPageNotFound') {. 
     ...
    url check functions
     ...

    $_SESSION["parameter"] = $parameter;
    $modx->sendRedirect($url);
}

Then in JS I check:

if($target.find('[data-parameter]').length > 0){
     ...
     url checking functions
     ...

     set URL without reloading the page
}

It works! But, I think it could be less complicated, it would be enough that there is a function in MODX that will open any page without changing the address and theoretically it is a function:

modX.sendForward

In the documentation it says:

Forwards the request to another resource without changing the URL.

But further, unfortunately, there is information that:

If the ID provided does not exist, sends to a 404 Error page.

So yes MODX will open any page without changing the address ( because it exists) but there it will check the unchanged URL (of which the page does not exist) and again redirects to a 404 rally so it loops. Is there a function in MODX similar to modX.sendForward which will open a page without changing the URL and after opening it will not check the unchanged URL?

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

OOOOOO yes! It works awesome! Thanks bro!
That’s exactly what I meant, it goes to the page without redirecting and there is a full address, e.g.:
exampleadomain.com/gallery/photo-1

I handle the showing of the photo in JS rally so just putting it in a plugin with a function was enough:
$modx->sendForward($id);

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.