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

    Hi there !

    OK I got a dynamic page based on some query parameters, let's say :

    http://whatever.com/modx-page.html?product=white-product
    http://whatever.com/modx-page.html?product=black-product
    http://whatever.com/modx-page.html?product=red-product

    Now, I want these URLs to look nicer, for example :
    http://whatever.com/modx-page-white-product.html
    http://whatever.com/modx-page-black-product.html
    http://whatever.com/modx-page-red-product.html

    How would you do that ? This is not just a 3 products things, there are 100's of products so this is not something I can do manually within the manager doing tricks or whatever with aliases.

    I came with the idea of rewriting with some Apache rules.

    Worked great with 2.1.5. Now 2 days ago I upgraded to 2.2.1 and the stuff is broken. Why ? Because of this piece of code in modxrequest.class.php / _cleanResourceIdentifier :

    $requestUri = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
    if ($this->modx->getOption('base_url', null, MODX_BASE_URL) . $identifier !== $requestUri) {
    $parameters = $this->getParameters();
    unset($parameters[$this->modx->getOption('request_param_alias')]);
    $url = $this->modx->makeUrl($this->modx->aliasMap[$identifier], '', $parameters, 'full');
    $this->modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
    }

    Basically it checks if the URL is what it's supposed to be (ie : is modx-page-white-product.html the correct URL ?), if not (that's the case, it should be http://whatever.com/modx-page.html?product=white-product), a redirect happens. Which make all my efforts to have nice URLs pointless.

    Question is, why was this added ? Any developer out there that could point the reason ? If there's a good reason or no developer can answer, can anyone think of an alternative solution to the problem ?

    Thanks !
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      From what I know that piece was added to make sure resources are only accessible via their set URI, adhering to strict SEO rules. Though I did think that was made optional through a system setting?

      Maybe you can post your rewrites, perhaps there's something we can change about that to satisfy your requirements.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 34665
        • 11 Posts
        Hey Mark,

        Thanks for the answer.

        Looking at the code, there's no settings check, which would have been great actually cause I had to hack the file, which sucks.

        The rewrite is pretty basic, takes the modx-page-(.*)\.html and redirects to index.php?q=modx-page.html?product=[whatever was caught by the regular expression]. I don't have the exact rule cause actually the example was just an example, in my site it's not about white or red products, it's a little bit more complicated but the rule works, no problem with that.

        The general idea / problem is that you can't anymore rewrite your URLs. I guess that I'm not the only one doing this rewrite trick so I was hoping someone had another solution.

        Actually I'll take a look at VisionCart, I'm pretty sure that they manage to do something to have nice URLs instead of ?productId=1, don't they ?

        Well anyway, again thanks for the answer, if you have another solution don't hesitate and making this a settings could definitely be great (if others have the same problem as me).

          • 18373 ☆ A M B ☆
          • 3,141 Posts
          ..takes the modx-page-(.*)\.html and redirects to index.php?q=modx-page.html?product..

          If it's done exactly how you sketch it there, you'll need to replace that second question mark with an ampersand (&).

          That code should only look at whatever is passed into the "q" url parameter, and not what comes after it like your &product=xyz
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 34665
            • 11 Posts
            Yeah my mistake, I did this quick to match the example but don't worry about the rewrite, it's not a rewrite problem, the rule is OK !

            When I debug the code, it compares "modx-page-white-product.html" with "modx-page.html", and because they don't match, does the redirect, which made the rewrite in the first place pretty useless.