homepage Welcome to WebmasterWorld Guest from 54.166.113.249
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How To Speed Up WordPress
hat tip to jdMorgan for this gem
incrediBILL

WebmasterWorld Administrator incredibill us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4502109 posted 7:20 pm on Sep 30, 2012 (gmt 0)

The previous Apache forum moderator and Apache guru jdMorgan posted this gem on how to speed up the default WordPress htaccess code which deserves more attention than being buried in a 2 year old thread.

The top version is for .htaccess vs the second version which would be placed in an httpd.include or vhost.conf file on many servers using control panels.


# if this request is for any of these filetypes
RewriteCond $1 \.gif$ [NC,OR]
RewriteCond $1 \.jpg$ [NC,OR]
RewriteCond $1 \.ico$ [NC,OR]
RewriteCond $1 \.css$ [NC,OR]
RewriteCond $1 \.js$ [NC,OR]
RewriteCond $1 \.mp3$ [NC,OR]
RewriteCond $1 \.wav$ [NC,OR]
RewriteCond $1 \.pdf$ [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]

Whereas in a server config file but outside of any <Directory> container, you'd want:

# if this request is for any of theses filetypes
RewriteCond $1 \.gif$ [NC,OR]
RewriteCond $1 \.jpg$ [NC,OR]
RewriteCond $1 \.ico$ [NC,OR]
RewriteCond $1 \.css$ [NC,OR]
RewriteCond $1 \.js$ [NC,OR]
RewriteCond $1 \.mp3$ [NC,OR]
RewriteCond $1 \.wav$ [NC,OR]
RewriteCond $1 \.pdf$ [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]

These changes have to do with differences in performance of regex-ORs versus RewriteCond [OR]s* due to the fact that code in server config files is "pre-compiled" at server start-up as opposed to being interpreted on-the-fly in .htaccess, differences in the "local URL-path" as seen by the RewriteRule, and --as stated above-- the fact that mod_rewrite processing is not re-started from the top after any rule matches, as it is in .htaccess.

In your situation, this code may not be much faster that the box-stock code provided by WP. But if you serve any images, media, or document files whatsoever, it will be faster nonetheless.

Jim


The original thread is here for more information about the hows and whys of this dicussion.
[webmasterworld.com...]

Basically, what the above attempts to accomplish is speeding up the delivery of any associated images, javascript, CSS or any other file required by the WordPress template.

 

mromero

10+ Year Member



 
Msg#: 4502109 posted 8:06 pm on Sep 30, 2012 (gmt 0)

Does this offer any efficiencies over W3 Total Cache for example?

incrediBILL

WebmasterWorld Administrator incredibill us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4502109 posted 9:44 pm on Sep 30, 2012 (gmt 0)

You're talking apples and oranges, but the answer is yes, it should add a marginal improvement to W3 total cache.

What Jim did offers efficiency in simply processing the file requests faster which happens in Apache before it ever gets to any cache servers. What that means is with this variation to the script, certain supporting file requests (images, javascript, etc.) will be sent to W3 Total Cache even faster than before.

The efficiency of W3 Total Cache is that it prebuilds page requests and stores the results so subsequent requests don't always trigger a complete SQL query to regenerate each time.

The combination of the 2 should be quite nice on a high volume site.

I've used the same basic script with some slight variations on a PHP site of mine to provide extensionless SE friendly URLs and it works really well.

ergophobe

WebmasterWorld Administrator ergophobe us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4502109 posted 5:32 pm on Feb 25, 2013 (gmt 0)

Just looking over this and you're missing Jim's .htaccess version.

What you have here is
1. a version for when you're in a virtual host context.
2. a version for when you're in a directory context

Both of these could be in httpd.conf or a vhosts.conf file (though #2 would have to be within a <Directory> and #1 would have to be within a <VirtualHost> section but not within a <Directory>). And #1 could be in .htaccess, but would not be as efficient as Jim's version specifically for .htaccess files.

The difference between #1 and #2 is whether or not you have a slash at the start of rules because vhost and directory contexts differ with regard to how they handle file paths.

.htaccess differs in an additional important respect - in .htacess, the code may get executed multiple times.

So in that case, you can get some additional savings by starting the whole thing out with a check for whether or not the request is for index.php and just stopping there. So Jim's code for .htaccess looks like this:

# 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


Note that one can achieve an additional efficiency by chaining RewriteCond ORs (as in the other examples) instead of using the regex ORs used here.

[edited by: tedster at 1:37 am (utc) on Mar 4, 2013]
[edit reason] fixed typo [/edit]

ergophobe

WebmasterWorld Administrator ergophobe us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4502109 posted 5:33 pm on Feb 25, 2013 (gmt 0)

And reference to the original thread for people who want to follow the full discussion

[webmasterworld.com...]


And, thanks to g1smd, the same techniques applied to

- drupal - [webmasterworld.com...]

- joomla - [webmasterworld.com...]

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4502109 posted 4:09 pm on Mar 4, 2013 (gmt 0)

Thank you!

I'm thinking I'll need to post something similar in the WP forum and add a ptr here to include this.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved