We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20981
    • 2 Posts
    Lately, I stumbled upon an issue related to the way friendly url are rewritten:
    The current .htaccess file defines the friendly urls rewrite rules as follows:

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


    Which means "if the requested file does not exist, or does not point to an actual directory, rewrite the url as index.php?q= ... followed by the original url path+parameters. Let be that the last rewrite action for this url". OK, so far so good. Now the problem:

    Suppose that the main page contains a reference to an image say "/assets/notthere.jpg". What will happen now is that while loading the main page, the client will try to GET the /assets/notthere.jpg image. The server will not find the file and rewrite it to a new page. Hence a new request for the main page will be made, which still will contain a missing image, triggering a new page request and so forth.

    In few words the rewrite rule is too forgiving. There should be a way to distinguish between

    http://mysite.com/a-friendlyurl-dir/a-friendlyurl-article
    and
    http://mysite.com/assets/notthere.jpg

    In principle you would like to redirect only the first and just issue a plain 404 for the second ...

    In my opinion, the best way would be to strengthen the rewrite rule as follow:

    # The Friendly URLs part
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !^.*/(assets|manager)/.+$
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    


    And possibly add any other directories which are not related to modx as well ...
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It does? I just tried it out, and it doesn’t seem to bother it at all that the image isn’t there.
        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
        • 20981
        • 2 Posts
        After (quite) some more digging I have found out that this happens in combination with the plugin "Image Resizer". When reading the image using fopen using the HTTP protocol mode it does not check for response headers, nor does it check for a proper

        We might want to move this thread to "plugins" ...
        Still, re-rendering a page for each missing image/file does not look right to me, even though a 404 header code is provided.

        I leave this philosophical matter to the gurus of modx.
        Thanks for testing it up anyway.
          • 22303 MODX Staff
          • 10,725 Posts
          My first inclination is to suggest you make sure all of the resources you are requesting as links in your pages are valid; I typically use HttpFox (a Firefox add-on) to see everything going on when requesting a page. If you are serving requests with this image type via MODx Resources (formerly Documents), this is your best bet.

          Now, if you do not need to serve image or other file types besides HTML, you can add an additional condition to the mod_rewrite directives provided by default in MODx. For example, I use this on a typical site to prevent bootstrapping the MODx request handler on every missing image, pdf, doc, etc.:
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|gif|css|png|js|ico|mp3|swf)$ [NC]
          RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

          The additional RewriteCond line simply says "AND the REQUEST_FILENAME does not end with .jpg, .jpeg, .gif, .css, ..., ignoring the case of the REQUEST_FILENAME". You can customize this for your needs or take it even further to detect other kinds of requests you do not want to be bootstrapped by MODx.