Better permalink rewrite code

Most blog powered by WordPress use ‘pretty’ links structure. This allows you to have hierarchical structure of the URL elements and that is much easier to read, and can even contribute to SEO results. And to achieve this WP adds rewrite code into htaccess.

Original WordpRess code is this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

And, this is usually left as it is, and it works just fine. But, this actually can be better and not only that, changing this piece of code can speed up your blog by avoiding some of the unneeded file and folder checks that rewrite module will perform based on the original WordPress code.

JP Morgan has suggested modified rewrite code to be used in the htaccess that will perform much better:

# BEGIN WordPress
RewriteEngine on
#
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive
# RewriteBase /
#
# if this request is for "/" or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpeg|jpg|png|ico|css|js)$ [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
RewriteRule . /index.php [L]
# END wordpress

On line 11 you see a number of extensions that will be excluded from rewrite checking. It’s not good to have too many extensions here because they cam have negative impact. Images, CSS and JS are enough to have here. This new code also removes IfModule check. If you already have mod_rewrite this check is useless, if you don’t have mod_rewrite than you need to turn of the permalinks in the WordPress. You can safely remove all lines starting with #, they are only comments.

Actually speedup gain depends on many factors: size of the website, speed of the server and things like that. If you have small blog, speedup will be small, but in many cases you will be able to notice it. JP Morgan claims that htaccess processing with changed code is cut in half and that has great positive effect on the website performance. I tested the code on two websites for now, and it works good, and some speed gains can be measured.

Source at WebmasterWorld.com: http://www.webmasterworld.com/apache/4053973.htm

Better permalink rewrite code, 5.0 out of 5 based on 1 rating

One Response to “Better permalink rewrite code”

  1. Kim | October 11, 2010 at 21:41

    Hi MillaN,

    I’m currently using a mix between a Drupal .htaccess and your modified WP .htaccess file.

    Just FYI but here’s my .htaccess file (the piece before your posted code that is):


    # Make WordPress handle any 404 errors.
    ErrorDocument 404 /index.php

    # Force simple error message for requests for non-existent favicon.ico.

    # There is no end quote below, for compatibility with Apache 1.3.
    ErrorDocument 404 "The requested file favicon.ico was not found.

    FileETag None

    # PHP 4, Apache 1.

    php_value magic_quotes_gpc 0
    php_value register_globals 0
    php_value session.auto_start 0
    php_value mbstring.http_input pass
    php_value mbstring.http_output pass
    php_value mbstring.encoding_translation 0

    # PHP 4, Apache 2.

    php_value magic_quotes_gpc 0
    php_value register_globals 0
    php_value session.auto_start 0
    php_value mbstring.http_input pass
    php_value mbstring.http_output pass
    php_value mbstring.encoding_translation 0

    # PHP 5, Apache 1 and 2.

    php_value magic_quotes_gpc 0
    php_value register_globals 0
    php_value session.auto_start 0
    php_value mbstring.http_input pass
    php_value mbstring.http_output pass
    php_value mbstring.encoding_translation 0

    SetOutputFilter DEFLATE

    # Requires mod_expires to be enabled.

    # Enable expirations.
    ExpiresActive On

    # Cache all files for 2 weeks after access (A).
    ExpiresDefault A1209600

    ExpiresByType image/jpg "access plus 1 months"
    ExpiresByType image/gif "access plus 1 months"
    ExpiresByType image/jpeg "access plus 1 months"
    ExpiresByType image/png "access plus 1 months"

    ExpiresByType text/css "access plus 1 months"

    ExpiresByType text/javascript "access plus 1 months"
    ExpiresByType application/javascript "access plus 1 months"
    ExpiresByType application/x-shockwave-flash "access plus 1 months"

    # Do not allow PHP scripts to be cached unless they explicitly send cache
    # headers themselves. Otherwise all scripts would have to overwrite the
    # headers set by mod_expires if they want another caching behavior. This may
    # fail if an error occurs early in the bootstrap process, and it may cause
    # problems if a non-Drupal PHP file is installed in a subdirectory.
    ExpiresActive Off

    VA:R_U [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

Follow us on Twitter

Social Networks

Google Plus Profile Subscribe to RSS feed LinkedIn Profile Follow me on Pinterest Watch our Youtube Videos Find us on Flickr
Hostgator
Get Clicky Web Analytics
Envato

Feedburner Feedburner updates

Sign up to receive news from this website to your email.

Dev4Press on YouTube

Archives