We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45892
    • 9 Posts
    I need to determine if a user accesses a page using www.example.com/index.php?id=100 for example (instead of www.example.com/friendly/path/to/page). Is this possible?

    I assume the change from index.php?id= to /friendly/path/to/page occurs in Modx itself.

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

      • 3749
      • 24,544 Posts
      This might do it:

      <?php
      if (strpos($_SERVER['REQUEST_URI'], 'index.php') !== false) {
         /* Not a friendly URL */
      }
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 45892
        • 9 Posts
        The apache logs show the index.php?id= first then the modified url:

        69.14.XX.XX - - [05/May/2014:19:49:03 +0000] "GET /index.php?id=659 HTTP/1.1" 301 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0"
        69.14.XX.XX - - [05/May/2014:19:49:03 +0000] "GET /modified/path/to/page/ HTTP/1.1" 302 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0"
        

        The only items I changed above are the IP addresses and the modified url in the second log entry.

        None of the $_SERVER variables seem to help. The $_SERVER['REQUEST_URI'] variable is "/modified/path/to/page/" not the initial string "index.php?id=..." from the 1st request.

        Quote from: BobRay at May 05, 2014, 10:39 PM
        This might do it:

        <!--?php
        if (strpos($_SERVER['REQUEST_URI'], 'index.php') !== false) {
           /* Not a friendly URL */
        }
        -->
        • discuss.answer
          • 4172
          • 5,888 Posts
          not sure, if that is what you want.
          You can try this in a plugin with the system-event onHandleRequest

          <?php
          if (isset($_GET['id'])){
          	$modx->sendForward($_GET['id']);
          }


          this doesn't do a sendRedirect, if there is ?id=XXX in the URL
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            If you are using mod_rewrite, then MODX never knows if the index.php was directly requested or not. Apache rewrites the URL before passing it on to MODX's index.php.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Unfortunately, for some reason MODX does an actual redirect, so the SERVER variables for the original request are lost.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 45892
                • 9 Posts
                The onHandleRequest might be what I'm looking for. Testing now.

                Quote from: Bruno17 at May 06, 2014, 12:57 AM
                not sure, if that is what you want.
                You can try this in a plugin with the system-event onHandleRequest

                <!--?php
                if (isset($_GET['id'])){
                	$modx--->sendForward($_GET['id']);
                }


                this doesn't do a sendRedirect, if there is ?id=XXX in the URL
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  So is MODX forcing every page to be requested twice by using a redirect if Friendly URLs is turned on?
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 3749
                    • 24,544 Posts
                    I think it's the other way around. The redirect only occurs when FURLs are off.

                    The FURL rewrite rule in .htaccess only comes into play when the URL is not found. So when Apache doesn't find the original URL, it gets re-written to index.php? + the alias before MODX gets a look at it. As I read the code, if the request handler can find the resource based on the alias, it will return it without a forward.

                    URLs already in the form index.php?id=## should bypass the FURL rewrite rules (since index.php exists), but then index.php (in the request handler) gets the uri from the resource, prepends the site_url and does a forward (sendRedirect()). OnHandleRequest gets called before any rewriting so Bruno17's technique should work.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Sounds like if furls are on, it'll redirect any non-fulr URLs.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org