We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • If we look into default .htaccess, we found some lines of code like below:

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


    These code will evaluate every request to our server, even if it's only try to get a small favicon.ico image, a small robots.txt file or "index.php" in the root directory.

    The code above perform 1 or 2 IO operation to harddisk: "-f" to check if the requested file is available on hardisk, then if it's not a file then followed by "-d" to determine whether the request is a physical directory or symlink.

    I think it's not needed and will add some microsecond (?) to every request, because IO operation known as 'bottleneck' beside database query.

    To avoid IO operation (checking existing file or folder), we should modify the code above.



    ALTERNATE 1: add .htaccess to physical sub-directories

    As found in manager folder, we could add the same .htaccess to every 'static' sub-directories. For example add it to "images" folder.

    #.htaccess
    RewriteEngine Off
    


    But it's such a wasting time, and this solution also add another IO operation because Apache will check every .htaccess in every subdir.



    ALTERNATE 2: add another RewriteCond to the default code

    Before making IO operation by -f or -d, we add another RewriteCond to evaluate the REQUEST_URI. This solution is much better because, we only have to manage one .htaccess file.

    Make sure you don't create ALIAS of resources same as existing physical subdir.

    # The Friendly URLs part
    
    # avoid IO on subdir: extras, media, themes, images, assets, connectors and manager
    RewriteCond %{REQUEST_URI} !^(/extras/|/media/|/themes/|/images/|/assets/|/connectors/|/manager/) [NC]
    
    # existing code below
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    


    But this code also still make an IO operation on non physycal file/folder and also on all files in root directory, such as favicon.ico, robots.txt and index.php.



    ALTERNATE 3: split the default code

    Split the code: the first part to check the files in root directory, and the second for subdir created by MODx resources. Also make sure you don't create ALIAS of modx resources same as existing physical subdir or file.

    # The Friendly URLs part
    
    # Checking files in root directory but only on .html, .txt, .xml and .rss
    # IO operation still used if pass the first RewriteCond 
    # but not applied, for example, to favicon.ico file.
    RewriteCond %{REQUEST_URI} ^/([a-z0-9\-_]+)\.(html|rss|xml)$ [NC]
    #If there are static html/rss/xml files in root, uncomment below
    #RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    
    # Checking subdir completely without IO operation
    RewriteCond %{REQUEST_URI} !^/(extras|media|themes|images|assets|connectors|manager)/ [NC]
    RewriteCond %{REQUEST_URI} ^/([a-z0-9\-_]+)/(.*)? [NC]
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    




    ALTERNATE 4: use another domain or subdomain for serving static files

    This solution is out of .htaccess part. So it's should be discussed in another thread wink



    Cheers,
    @zaenal [ed. note: lokamaya last edited this post 11 years, 5 months ago.]
      zaenal.lokamaya
    • Ah... I give a wrong title! wink

      This not only improve static files, but also improve page load of MODx resources in frontend. Because no more IO operation to output MODx resources (-f or -d flag) especialy in subfolder, it's only evaluate the REQUEST_URI on the fly (memory/RAM).

      As you can see in Ryan Dahl's "Introduction to Node.JS" video at https://www.youtube.com/watch?v=M-sc73Y-zQA, today computer latency is as follow:


      • L1: 3 cycles
      • L2: 14 cycles
      • RAM: 250 cycles
      • Hard disk: 41,000,000 cycles
      • Network: 240,000,000 cycles
        zaenal.lokamaya
        • 42046
        • 436 Posts
        Thanks for this.

        However I've found using method 3 broke a PrettyPhoto lightbox implementation through the Gallery addon. Not sure why, any ideas?
        • Where do you put the images for the Gallery addon? Do you use 'real' static images or using MODx static Resource? [ed. note: lokamaya last edited this post 11 years, 4 months ago.]
            zaenal.lokamaya
            • 42046
            • 436 Posts
            The images are uploaded through the Gallery addon and reside under the assets folder. Gallery appears to pull them through a connector php script referencing the image file location.

            The html on the page looks like

            <a href="/assets/gallery/2/5.jpg" class="view" data-rel="prettyPhoto[gallery1]" rel="prettyPhoto[gallery1]"></a>
            • It's better to have full url to your images, i.e. http://www.mydomain.com/assets/gallery/2/5/jpg, or try to remove trailing slash before assets.

              Is there base href in your html code?
              <base href="http://mydomain.com/" />


              With or wihout base href, the url to your image will be requested as:
              http://www.mydomain.com//assets/gallery/2/5/jpg

              Then the rewriteCond
              RewriteCond %{REQUEST_URI} ^/([a-z0-9\-_]+)/.....

              fail to evaluate the double slash before assets, and make your image broken.
                zaenal.lokamaya
              • how fast did you get the improvement?
                btw,
                RewriteCond %{REQUEST_URI} ^/([a-z0-9\-_]+)\.(html|rss|xml)$ [NC]

                doesn't apply for non-ASCII alias, and non-extension resources (many like to eliminate it to embrace changes on parent-child structure).

                  Rico
                  Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                  MODx is great, but knowing how to use it well makes it perfect!

                  www.virtudraft.com

                  Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                  Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                  Maintainter/contributor of Babel

                  Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.