MODx Plugin for Resource Redirecting

So Abraham got to work and adapated the example Page Not Found MODx Plugin to work as follows: domain.com/19 forwards to (301) domain.com/category/resource-19-alias.html .

The Plugin Code

Create a new plugin, name it whatever you'd like resourceRedirect. Then on System Events check OnPageNotFound.

    <?php
if ($modx->event->name == 'OnPageNotFound') {
$output = '';
$param_alias = $modx->getOption('request_param_alias');
$get = $modx->getOption('GET', $modx->request->parameters, '');
$rId = $modx->getOption($param_alias, $get, '');
if(!is_numeric($rId)) {
41return $output;
}
$rId = intval($rId);

$resource = $modx->getObject('modResource', array('id'=>$rId));

if($resource) {

$url = $modx->makeUrl($resource->get('id'));
$modx->sendRedirect($url);

return $output;
}
} 

Recent Posts

MODX Cloud NGINX Rule for ssl and www redirect

Replace "domain.com" with your domain. This example uses www as the end point, if you prefer non-www just change the two domain variations around in the first if statement. if ($host = "domain.com") { return 301 $scheme://www.domain.com$request_uri; } if…

PDF From Adobe Illustrator not showing a mask transparency right on macOS Preview or Windows? Here is the fix

So you sent a small preview PDF to the client, it looks great. Then you export the Press Quality version and it looks different, why? Let's dig in.

Improving MODX Page Speed for Google

Your website speed is impacting your bottom line , here are a few ways to improve it through browser caching, asynchronous loading of resources and other tips and tricks.