We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42718
    • 2 Posts
    We're replacing a .asp website with MODx. I'd like to setup 301 redirects for the twelve or so .asp pages that will no longer be present.

    One thought that would require some programming... the 404 not found handler could look for references to the old filename.asp pages and appropriately 301 redirect to the new MODx page.

    Is this the right way to go or is there a better solution?

    This question has been answered by tcbutterworth. See the first response.

      • 29019 ☆ A M B ☆
      • 104 Posts
      You could create a plugin and have it run on the "OnPageNotFound" system event. You could have a database of old paths or some kind of file or just hard code the paths in your PHP, but once you figure out where they need to go, your redirect could look something like this:

      <?php
      // find your $resource_id
      
      $url = $modx->makeUrl($resource_id);
      $modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
      
        www.darkstardesign.com

        Are you in the Nashville area? Join us for our next MODX meetup!
      • discuss.answer
        • 42450 ☆ A M B ☆
        • 10 Posts
        Tim Butterworth, MODX Ambassador Reply #3, 13 years, 8 months ago
        Two ways to achieve this:

        1. Good old fashioned .htaccess
        2. As per Scott's response, if you need to have any logic applied, add a simple plugin that picks up the pageNotFound system event. I typically end up with a combination of the two.

        If you need to get more complex, it is also easy in the plugin's to code to route the users to specific resources based on parameters coming in. For example you can read the request parameters and route to a different page accordingly. You may want to set up a database of from/to pages as per Scott's suggestion. Also, you can set $_GET parameters that the page will then be able to access. And you simply use the $modx->forward() call to route to the desired resource:

        $requestParts = explode('/', $requestURI, 3);
        if (count($requestParts) >= 1) 
        {
                if ( $requestParts[1] == 'product' )
                {
                      $_GET['action'] = 'product';
                      $modx->sendForward(6);
                }
                if ( $requestParts[1] == 'review' )
                {
                      $_GET['action'] = 'review';
                      $modx->sendForward(7);
                }
        }
        else
                $modx->sendForward(3);
        
          Tim Butterworth, MBA
          MODX Ambassador
          http://www.eecsoftware.com
          • 33987 ☆ A M B ☆
          • 197 Posts
          We use a very simple plugin for this in combination with one TV. The TV is a textarea which we call '301 redirect'. The webmaster can add one 'old URL' per line in there. If the old URL is 'http://www.site.ltd/folder/another-folder/old-page', then the entered URL should in the textarea line should be: folder/another-folder/old-page

          The plugin fires on the event 'PageNotFound' and has the following code (please note you have to change the ID mentioned in here to the ID matching your 301 TV):
          <?php
          // Retrieve current URL
          $search = $_SERVER['REQUEST_URI'];
          if($search[0] == '/'){
          	$search = substr($_SERVER['REQUEST_URI'], 1);
          }
          
          // Search TV values table for matches
          $sql = 'SELECT tmplvarid, contentid, value FROM modx_site_tmplvar_contentvalues WHERE tmplvarid = 19 AND value LIKE "%'.$search.'%"';
          
          // Found? Redirect it with a 301 status code
          foreach($modx->query($sql) AS $row){
            $modx->sendRedirect($modx->makeUrl($row['contentid'],'','','full'),array('responseCode' => 'HTTP/1.1 301 Moved Permanently')); 
          }
            Sterc Internet & Marketing | MODX Founding Partner | Chairman of the MODX Advisory Board

            In need of a MODX consult? Try our MODX Developers Experts!
            • 22840
            • 1,572 Posts
            Wow so complicated for 2 lines of code in htaccess to save modx having to process it, just give the new pages the same file name so that the only thing that changes is the .asp to .html then add the below to your htaccess file

            RewriteEngine On
            RewriteRule (.*)\.asp $1.html [R=301]

              • 10208 ☆ A M B ☆
              • 1,780 Posts
              Or just use Redirector from package management.
                Frogabog- MODX Websites in Portland Oregon
                "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                Having server issues? These guys have MODX Hosting perfected - SkyToaster
                • 33987 ☆ A M B ☆
                • 197 Posts
                Quote from: frogabog at Feb 10, 2013, 12:16 PM
                Or just use Redirector from package management.

                The difference with the redirector component is that the client can now manage 301's in the resource itself.
                  Sterc Internet & Marketing | MODX Founding Partner | Chairman of the MODX Advisory Board

                  In need of a MODX consult? Try our MODX Developers Experts!
                  • 10208 ☆ A M B ☆
                  • 1,780 Posts
                  Quote from: nomark at Feb 10, 2013, 02:14 PM
                  Quote from: frogabog at Feb 10, 2013, 12:16 PM
                  Or just use Redirector from package management.

                  The difference with the redirector component is that the client can now manage 301's in the resource itself.

                  Unfortunately a tv like that would only benefit me. My clients would never imagine themselves interested enough to know the need. So far at least.

                  Setting 15-30 301 redirects in one shot when migrating to MODX, I find the Redirector component is mighty handy.
                    Frogabog- MODX Websites in Portland Oregon
                    "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                    Having server issues? These guys have MODX Hosting perfected - SkyToaster
                    • 33987 ☆ A M B ☆
                    • 197 Posts
                    Yea, if you just need 15-30 redirects, it's overkill to do the TV. I would train your clients in the whole 301-thing though. It's an essential part of SEO. If they start changing URL's on a website they are loosing ALL ranking built on those old URL's... It's pretty easy to make them understand that.

                    Quote from: frogabog at Feb 11, 2013, 04:23 AM
                    Quote from: nomark at Feb 10, 2013, 02:14 PM
                    Quote from: frogabog at Feb 10, 2013, 12:16 PM
                    Or just use Redirector from package management.

                    The difference with the redirector component is that the client can now manage 301's in the resource itself.

                    Unfortunately a tv like that would only benefit me. My clients would never imagine themselves interested enough to know the need. So far at least.

                    Setting 15-30 301 redirects in one shot when migrating to MODX, I find the Redirector component is mighty handy.
                      Sterc Internet & Marketing | MODX Founding Partner | Chairman of the MODX Advisory Board

                      In need of a MODX consult? Try our MODX Developers Experts!
                      • 10208 ☆ A M B ☆
                      • 1,780 Posts
                      You make a good point. Food for thought, thank you.
                        Frogabog- MODX Websites in Portland Oregon
                        "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                        Having server issues? These guys have MODX Hosting perfected - SkyToaster