0
votes

I am rebuilding a website in ModX and I want to redirect the old URLs to the new ModX pages, automatically.

An old URL is of the form, http://www.oldsite.com/?pg=2

Every page is like this, so I need to manually map the old page IDs to the new ModX resource IDs. For example, pg=2 is the contact page, which is now resource ID 11, so I'll end up with a map like [2=>11, 3=>15, etc]

If I tweak the main index.php right in the docroot, this does exactly what I want:

/* execute the request handler */
if (!MODX_API_MODE) {
    if (isset($_GET["pg"])) {
        if ($_GET["pg"] == 2) {
            $url = $url = $modx->makeUrl(11);
            $modx->sendRedirect($url);
        }
        else {
            // Page is set, but we don't have a redirect for it.
            $modx->handleRequest();
        }
    }
    else {
        $modx->handleRequest();
    }
}

However, I am not happy with hacking index.php directly. I'm a bit short of ModX experience to know exactly where to put this code. I tried:

  • A snippet, which I then called from my HTML header before any HTML, but the redirect stopped working
  • The Redirector extra, but this doesn't work on the QUERY_STRING, I don't think

Any insight is appreciated, for the best place to package this code, or a pointer towards an Extra I should be using.


The solution that worked for me, following Sean's insights below, is a plugin. The plugin code is below. For other plugin newbies like me, ensure you visit the "System Events" tab to enable your plugin for the event you're trying to access.

<?php

if ($modx->event->name == 'OnWebPageInit') {
    // Look to see if the GET params include a pg. If they do, we have a request
    // for one of the old pages.
    if (isset($_GET["pg"])) {

        // Map the old pg IDs to the new resource IDs.
        if ($_GET["pg"] == 2) {
            $url = $modx->makeUrl(11);
        }
        // Add more here...

        // When done trying to match, redirect.
        // But only do the redirect if we found a URL.
        if (isset($url)) {
            $modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
            exit;
        }
    }
}
1

1 Answers

1
votes

My preference to do this is in the .htaccess file with redirects or url rewriting - that way you can send the redirect and the response code ~before~ modx has to process anything [save a bit of overhead]

if you still want to do this in modx, take a peek at the sendRedirect docs & send the correct response code [so google gets the hint that the page has actually moved] Note: the $responseCode option is depreciated and you should use it in the options array these days:

$modx->sendRedirect('http://modx.com',array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));

I do agree with not hacking the index.php file, only will cause you grief. What you want to do is place your redirect code in a plugin. Check the Modx API docs for the appropriate event for it to fire on - perhaps: OnWebPageInit will do the trick. Sorry, I don't know exactly which one will work.

HOWEVER ~ IMPORTANT NOTE!

Not all events are actually active, they may show up in the modx manager but don't actually do anything, you will just have to test or dig through the code to find out. [or ask in the community] Again, sorry, I don;t know for sure which ones work and which don't.