pepebe
7/7/2015 - 7:28 AM

Redirect based on id

Redirect based on id

<?php
/*
By Abraham Sewill
Source: http://himmdesign.com/blog/2013/01/24/modx-plugin-for-resource-redirecting/

Redirect your_site.com/120 to your_site_com/page_with_id_120.html
*/

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)) {
		return $output;
	}
	$rId = intval($rId);

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

	if($resource) {

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

		return $output;
	}
}