Support » Requests and Feedback » More efficient rewrite rules

More efficient rewrite rules

  • # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    The default WordPress .htaccess code is non-optimum. With the -f and -d “exists” checks appearing first in the list, every URL request arriving at the server will result in at least two very slow physical disk reads to see if the URL resolves to an existing file. Since every page consists of multiple stylesheet and image requests, there could be several dozen very slow -f and -d “exists” checks for every page requested.

    Since URLs for images and stylesheets are never going to be internally rewritten to the index.php script, all of those requests should simply be rejected for rewriting by using a RewriteCond that checks the file extension of the URL request. The -f and -d “exists” checks are never required for image or stylesheet requests.

    New code is shown below. In this version, requests for images and stylesheets no longer invoke the slow and inefficient -d and -f “exists” checks. This change dramatically speeds up mod_rewrite processing.

    In the default mod_rewrite code, there is a second similar issue. After a URL request has been rewritten to the index.php script, mod_rewrite is invoked again, and it checks to see if index.php exists as a file or directory using those very slow -f and -d checks again. This is a crazy waste of server resources. Of course it exists! Every time! The problem is solved by adding a negative match RewriteCond checking if the current path is /index.php or not. This change speeds up mod_rewrite processing even more.

    The list of file extensions !\.(gif|jpe?g|png|ico|css|js)$ should be optimised “per site” such that the most requested file extensions are listed first. Additional file extensions can be added, such as .zip, pdf, doc, mp3, swf, flv, wmv, avi, mov, mpeg, doc, xml and others, but these should only be added if actually used somewhere on the site. Each addition degrades server performance very slightly, so keep the list lean.

    The replacement .htaccess file, shown below, should result in at least a three-fold increase in mod_rewrite processing performance and will lead to longer hard drive life. It may well stave off a server upgrade on a busy site for a very long time.

    # Redirect requests for non-blank, non-canonical hostnames to canonical hostname
    RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    # BEGIN WordPress
    # Except for requests for /index.php and for the most-frequently-requested
    # filetypes that WP cannot generate, rewrite all URL requests which do not
    # resolve to an existing file or directory to the WordPress script filepath
    RewriteCond $1 !^(index\.php)?$
    RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ /index.php [L]
    # END WordPress

    I am working on a patch for Trac.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘More efficient rewrite rules’ is closed to new replies.
Skip to toolbar