Optimized rewrite rules for WordPress

Permalinks are one essential part of any WordPress installation to have a example.com/nice-url/ instead of example.com/?p=378.The translation of the ugly looking URL into the cute URL is performed by the Apache server instructed by the rewrite rules in the .htaccess file. Even though WordPress can write the proper rules, there are some rules that are said to optimize the redirection and thus the page loading time.

Having found these rewrite rules for the WordPress .htaccess in a local installation, I remembered that while researching some ways to optimize WordPress speed and load up times, I found these rules in a forum.

# 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¦jpg¦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

Unfortunately, I can’t seem to locate the forum again (so if you know the user who wrote them let me know so I can quote him). These rules are said to speed up page loading times and you can see from the code that they are much more strict than those created by WP itself making explicit redirection rules for certain file types. From what I’ve tested them, they work pretty much in the same way than usual rules. So it’s your turn, what kind of rewrite rules do you use? How do WP standard rules perform compared to your own?

3 thoughts on “Optimized rewrite rules for WordPress”

  1. Wow this is an amazing piece of code. Thank you for the share, I noticed a large improvement with load time on client sites.

Leave a Reply to Ankur Cancel reply