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

    I made a page (ressource id 14) who show a product from a custom data base (really easy with XPDO, MIGX, ROWBOAT, GETPAGE !).
    The page (ressource id 14) return a HTTP RESPONSE HEADER Status: HTTP/1.1 200 OK
    The URL rewrite is turned on !

    Without URL rewrite my URL looks like this
    URL : http://mywebsite.com/index.php?id=14&asp=13878524

    With URL rewrite my URL looks like this
    FURL : http://mywebsite.com/aliasofresource14/?asp=13878524

    I would like to know how i can change the URL on the fly to look like something like this :
    http://mywebsite.com/aliasofresource14/product/13878524/something-for-search-engine.html

    I have read a lot of posts without finding a way to do it. Some posts are very old (more than 5 years) and other are not really similar because they are most based on the OnPageNotFound.
    I have try CustomUrls without success ! Maybe this extra is not good in my case ?!

    Thanks in advance for help ;-)


    This question has been answered by m.engel. See the first response.

      • 40706
      • 128 Posts
      We have done both options several times.

      I like the Plugin-Version with OnPageNotFound a bit more. Its easier to port from one Server to another, and has the ability to be much smarter.

      1. You can lookup in the Database for the right DocumentId or use modx->findResource('aliasofresource14/'), if the alias changes so you don't have to modify the plugin

      2. Decode the Url out of $_GET['q'] or $_SERVER, detect if the request isn`t malformed.

      3. Use $modx->sendForward(14); to redirect to the right page


      We have an Simple Article Router in work which has just categorypage/articlenumber.html and are using an redirection like the following. Which just redirects Articles to the category page.
      // a simple example how an 404 routing is done
      
      $catAlias = substr($_GET['q'], 0, strrpos($_GET['q'], '/') + 1);
      if ($categoryId = $modx->findResource($catAlias)) {
                      $_GET['article'] = substr(basename($_GET['q']), 0, -5);
                      $modx->sendForward($categoryId);
      }
      
        • 28432
        • 372 Posts
        First thanks you for answering ;-)

        In your exemple you use sendForward but if i understand (I'm bad at English) what is write in the RTFM https://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.sendforward
        $modx->sendForward(234);

        Send the user to Resource ID 234 without actually changing the URL.

        Sorry but i don't understand how it will change URL on the fly.

        1. I got already the value of $_GET['asp'] (i'm already showing different product based on the $_GET['asp'] value)
        2. detect if the request isn`t malformed => if something wrong with this $_GET value i'm not showing product
        3. the page of my product is always the same page (ressource with ID 14) like i said in my first post [ed. note: Steeve last edited this post 10 years, 4 months ago.]
          • 13428 ☆ A M B ☆
          • 1,031 Posts
          Try the CustomRequest extra for this: http://jako.github.io/CustomRequest/usage/
            • 28432
            • 372 Posts
            Quote from: Jako at May 19, 2016, 12:40 PM
            Try the CustomRequest extra for this: http://jako.github.io/CustomRequest/usage/
            Thank you for answering

            CustomRequest is installed but i don't make it work.

            I've try to make a test with pagination like you explain it there http://jako.github.io/CustomRequest/usage/#example-configurations without success.

            Configuration Name: Pagination
            Alias Path: #.*?(page/)#
            Resource: empty
            URI Parameter: ["page"]
            Regular expression: #(\d+)#
            (Image 1.jpg)

            To find the problem i turn on debug but i got nothing in the error log.
            I checked regular expression (#.*?(page/)#) to https://regex101.com/ and it return error even if it's blue inside the extra management (image 2.jpg).

            Best regards.


            • discuss.answer
              • 40706
              • 128 Posts
              The URL building is not part of the Plugin. Just do it in your chunks.

              http://mywebsite.com/aliasofresource14/product/13878524/something-for-search-engine.html
              Like -->
              [[~14]]product/[[+product_id]]/[[+url_thing]].html


              This URL does not exist, so OnPageNotFound kicks in, and redirects the user to the document 14 without changing the URL to http://mywebsite.com/aliasofresource14/

              The other method of redirecting to document is sendRedirect() which causes an 301 Redirect to another URL. This method is also capable of doing redirects by other typs like meta. Thats the difference to sendForward()
                • 28432
                • 372 Posts
                Hello,

                I have misunderstood sendForward Class Reference.
                So i make url like i desire : /parent/child/uniqueNumber/something-for-google.html and that make a 404 http response (before it was 200 OK status).
                Then with a plugin on event system OnPageNotFound redirect to my page (ID 14) using sendForward.

                Thanks a lot Michael ! Now i know how to make it easy ;-)