We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Hi all,

    I’m having a little trouble writing a fairly simple plugin that handles redirects, and would love some help.

    Basically, I’m trying to create short user URLs by redirecting site.com/username to site.com/viewprofile?username. I’m doing this by triggering the plugin on the OnPageNotFound system event, but what I can’t figure out is how to test if the URI entered is a valid user. All of these users will be part of a user group called "Members," so i can test against that, if it helps. Here’s a simplified version of what I have so far:

    <?php
    $search = $_SERVER['REQUEST_URI'];
    $search = ltrim($search,'/');
    if ($search) {
            if ($search == 'sitemember') {
    	       $modx->sendRedirect('viewprofile?user='.$search);
            }
    }
    return;


    The "($search == ’sitemember’)" bit is obviously the spot I can’t figure out. Can I use something like getObject(’modUser’), and if so, can someone show me the correct syntax? Otherwise, $user->isMember(’Members’) seems like it could work, but that seems like it only works for the current user.

    Thanks for the help!
    • Also, one bonus (although not essential) would be to either tie this into splittingred’s Redirector plugin, or at least have Redirector kick in after this executes. Is there a way to set plugin execution order, and/or write a line that lets actual 404s still be processed as 404s?
      • With some help from @mark_hamstra/@FreelanceWebDev on Twitter, I’ve gotten this far:

        <?php
        /* handle redirects */
        $search = $_SERVER['REQUEST_URI'];
        $search = ltrim($search,'/');
        if ($search) {
        	if($modx->getObject('modUser',array('username' => $search))) {
        		
        		$modx->sendRedirect('viewprofile?user='.$search);
        	}
        }
        return;


        This works, kind of. It does the redirect, but it’s doing it for every URI, not just ones that are members.
        • Hmm...strange. It started working now. Must’ve been a lingering cache issue in MODx somewhere. The last code I posted works great. Thanks!