We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9549
    • 93 Posts
    Instead of adding a new topic I might aswell post my problem here.

    I’m making a big website which works as a register with over a 1000 posts. Ideally it wouldn’t fit as MODx documents, as it would slow the server down and generate loads of cache. So I saved all my records in a separate database to store it all.

    So the question, how do I get all my posts to be friendly URLs, or be unique documents at all? Currently I just output the ID field and put it to a querystring..

    $output .= '<li><a href="index.php?id=' . $row['id'] . '" />' . $row['name'] . '</a></li>';
      • 22303 MODX Staff
      • 10,725 Posts
      No, you should have created a new topic.

      And I really have no idea what you mean. MODx Friendly URLs are designed as they are. You’ll need to design your own rewrite rules for your custom routing code.
        • 9549
        • 93 Posts
        Ok, sorry for the confusion.

        I have an "alias" field in the other database, like "alias" in the modx_site_content. How do I design the rewrite rules to output the alias field (from my custom database/table) instead of the id numbers (e.g. index.php?id=1023)? In MODx I can just write [~23~] and it outputs the FURL, but for a content from a separated table it get’s a bit harder smiley

        Another question, would it be easier if I just had each record as MODx document (as I did earlier)? I read somewhere in the forums that it’s not optimal with over like 200 documents or so. I have plenty of TVs connected to each document so it would add upp dramatically in the database.
          • 9549
          • 93 Posts
          I’m up for the latter. I’m not that good at mod_rewrite and regex, but I however found some clues from a Google search. http://www.biorust.com/tutorials/detail/226/en/ was a good read.

          I’ve changed my .htaccess accordingly but I’m little curious about the php script that does the job.

          My rewrite rule:
          RewriteRule ^artiklar/([0-9]+)(.*)$ rewrite.php?id=$1 [QSA]


          ..and php (simple test)
          // Get ID.
          
          $_id = $_GET[ 'id' ];
          
          // Print Yes if ID is greater than 10,
          // or No if ID is less than 10.
          
          if( $_id == 1399 ) {
          echo 'Yes';
          } else {
          echo 'No';
          } 
          


          but to redirect to the correct recordset, does that mean I have to query the sql for the "alias" field and match with the id? that would be a lot of queries generated. And how do I just use the url "artiklar/korv" instead of "artiklar/1399"?


            • 22303 MODX Staff
            • 10,725 Posts
            *blink*

            You don’t have to do anything in PHP except retrieve your additional query string parameters. The URL is rewritten, but it is still making the query string available. You need to add additional parameters to the query string; you can’t change the way the ?id= or ?q= part works.
              • 9549
              • 93 Posts
              Ah! Thanks, Jason. No wonder why I didn’t get my head around it, haha...
                • 9549
                • 93 Posts
                I got it to work now but I think these 2 rules are colliding with each other...

                RewriteRule ^store/(.*).html$ index.php?id=9&storeid=$1 [QSA]
                RewriteRule ^(.*)$ index.php?q=$1 [L, QSA]    <-- (MODx rule)


                because now I get a "Internal Server Error" smiley
                  • 9549
                  • 93 Posts
                  Anyone know how to get this to work?
                  It seems that I can’t use multiple QSA flags (which I need)

                  or that the pattern "(.*)" in the MODx rule is overriding my own..
                    • 13481
                    • 97 Posts
                    Your rules are colliding because you didn’t set the L flag on your first rule. The L flag makes that rule the last to be processed, and if you don’t set it, then it just skips right down and does the next one.  Also, if you are overwriting the query string then you don’t need QSA since that appends your new querystring on to the original one.  You might be getting a 500 error because you need separate RewriteCond for each rule (at least I can’t get it to work with just one set).

                    Here is a working example:

                    # The Friendly URLs part
                    RewriteCond %{REQUEST_FILENAME} !-f
                    RewriteCond %{REQUEST_FILENAME} !-d 
                    RewriteRule ^locator/dealers/([^/]*)/?(.*)?$ locator/dealers.html?s=$1&c=$2 [L] 
                    
                    RewriteCond %{REQUEST_FILENAME} !-f
                    RewriteCond %{REQUEST_FILENAME} !-d
                    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
                    


                    ’locator’ and ’dealers’ are pages in modx (parent and child respectivly) and I want locator/dealers/ne/lancaster to go to the page that pulls up dealers for Lancaster county in Nebraska.  However, I don’t want to make MODx pages for this because I have a database table of counties that I’m using.

                    This could be accomplished by calling the dealers pages like this:

                    locator/dealers?county=lancaster&state=ne


                    but I want a friendly url.

                    So in order to make a working friendly url like this: locator/dealers/ne/lancaster I need to place a rewrite rule before the rule for modx and make sure it is set to be the last rule processed.  The above code accomplishes that.  It also makes sure that urls like locator/dealers/ne and locator/dealers/ne/ work as well since the second slash and remainder of the url are optional. 

                    I’m using a snippet on the dealers.html page to grab the $_GET variables ’s’ and ’c’ and using them to find my county in the database and any pages associated with that county.  A really cool thing to do on these custom database driven pages is to reset the pagetitle variable to be something unique.  This makes these pages appear as unique as if you made them in MODx. Here is the code to do that as a plugin.

                     
                    if (isset($_GET['s']) && isset($_GET['c'])) {
                      $county = str_replace('-',' ',ucfirst($_GET['c']));
                      $state  = strtoupper($_GET['s']);
                      $query = "SELECT * FROM counties WHERE state = '$state' AND county = '$county'";
                      $result = $modx->db->query($query);
                      if (mysql_num_rows($result) > 0) {
                        while($row = mysql_fetch_assoc($result)) {
                          $id = $row['id'];
                          $state = $row['state'];
                          $county = $row['county']; 
                          $modx->documentObject['pagetitle'] = "Available Dealers in $county County, $state"; 
                        }
                      }
                    }
                    


                    For the System Event choose: ’OnLoadWebDocument’ and give it any name you want. Note that I check the database to see if the county and state actually exist in the database before setting the pagetitle. This is to prevent someone having fun on my page and injecting whatever they want for a pagetitle. That might sound harmless until those pages get into Google.

                    Hope this helps.

                    James

                      • 22303 MODX Staff
                      • 10,725 Posts
                      Thanks a million devtrench for sharing your solution for this. We are working on ways to make this possible with much less effort in the future, but in the meantime, this is a wonderful example of how to accomplish this here and now.