Remove query strings from static resources

Home Forums BulletProof Security Pro Remove query strings from static resources

This topic contains 15 replies, has 3 voices, and was last updated by  jenni101 2 years ago.

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #22722

    jenni101
    Participant

    Hi,

    I’m still trying to reduce our (awful) page load times, have looked into minifying plugins and cacheing plugins but read here that it can mess up your site big time – and have read that this will help instead:

    Remove query strings from static resources – Resources with a “?” in the URL are not cached by some proxy caching servers. Remove the query string and encode the parameters into the URL for the following resources:….

    The code options I’ve seen are below, and that go in the child theme functions.php are:
    What I’d like to check with you is:
    1. is this Ok to add to my child functions.php? As in, it won’t mess with BPS?
    2. Do you know which is the best version of the code to use?

    function _remove_script_version( $src ){
    $getMyUrl = site_url();
    $check = strpos($src, $getMyUrl);
    if ($check !== false) {
    $parts = explode( '?', $src );
    return $parts[0];
    }else{
    return $src;
    }
    }
    add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
    add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
    
    Or...
    
    function _remove_script_version( $src ){
    $parts = explode( '?', $src );
    return $parts[0];
    }
    add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
    add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
    
    Or...
    
    /*** Remove Query String from Static Resources ***/
    function remove_cssjs_ver( $src ) {
    if( strpos( $src, '?ver=' ) )
    $src = remove_query_arg( 'ver', $src );
    return $src;
    }
    add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
    add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

    All advice much appreciated,
    Jenny

    #22729

    AITpro Admin
    Keymaster

    I did a Google search for:  Remove query strings from static resources and I see there is a WordPress plugin with this name that does this.  I have never heard of or was aware that Query Strings had any impact on page load speeds.  So I cannot really offer any advice since I am not sure if that is really valid or not or how much of a speed increase you can expect or anything at all regarding this.  I do not think doing this will affect the BPS Pro plugin directly, but not really sure since I have never heard of this before.
    https://wordpress.org/plugins/remove-query-strings-from-static-resources/

    If you are manually editing any website files then follow the standard AutoRestore procedural steps: http://forum.ait-pro.com/forums/topic/autorestore-quarantine-guide-read-me-first/#procedural-steps

    #22736

    jenni101
    Participant

    OK, I’ll do a trial of this code and let you know the outcome – maybe a few days as our server is running some tests at their end too, to try to improve things.

    Cheers.

    #22752

    jenni101
    Participant

    If anyone else is interested I’ve found some more concrete info about this code from the plugin author of ‘Remove Query String’, here: http://www.paulund.co.uk/remove-query-string-stylesheets and i will feed back my results…

    #22810

    jenni101
    Participant

    Update: I’ve just retested my page load after adding this code (from the plugin author as above):
    and it now loads 5 – 6 secs faster for first load and 2 secs faster for repeat loads! Admittedly my site was super slow (hence trying everything to fix it) and I’ve been avoiding using caching and minifying plugins, but this still seems impressive!
    And so far I’ve not seen any problems/errors…

    function pu_remove_script_version( $src ){
    return remove_query_arg( 'ver', $src );
    }
    add_filter( 'script_loader_src', 'pu_remove_script_version' );
    add_filter( 'style_loader_src', 'pu_remove_script_version' );
    #22815

    rafaelmagic
    Participant

    jenni

    Usually query strings removal improves load times by milliseconds and not seconds. You might have tested load speed time during non peak hours.

    Ultimately to improve speed, it might be several moves that will fix things. Php version, caching, image optimization, finding bad plugins, SQL Tunning, lazy loading images, ttfb, object caching, deferring Java, etc.

    Caching is necessary with WordPress which is dynamic and it’s backend is SQL. For example even WordPress.com itself is caching using BatCache and Varnish. Their backend is also running Nginx.

    If you share a Website address that you want to improve, some of us might be able to help by analyzing using online pagespeed tools.

    #22859

    jenni101
    Participant

    @rafaelmagic – thanks for your helpful comments and suggestion to look at our site load time. It’s this url: http://www.the-photohub.co.nz

    There’s heaps of advice about using minifying and caching plugins, and I realise that our site would really benefit from using both but from what I understand the minifying bit doesn’t work well with Buddypress as it needs to load its scripts first, and that there are issues around managing the plugin firewall when using a minifying plugin; plus I’ve been really nervous of attempting to using w3TC again, since really screwing up another site when I installed it with BPS. But I guess I’ll have to look at this again soon…

    But any advice would be great! Thanks.

    #22862

    rafaelmagic
    Participant

    Ran your site thru Gtmetrix and Pingdomtools. Your site is loading really slow. 10 seconds is killing your Google rankings. They are reporting a 301 header issue, which is a redirect. So are your redirects by .htaccess or a Plugin? It’s slowing your site by 3 seconds.

    So now turn OFF ARQ so we could add a caching plugin. Since those caching files are always changing you have to exclude that directory in BPS Pro. Directory is wp-content/cache

    Then wp-Config has to be unlocked because Caching plugins will add a line of text.

    Assuming your in a Shared Host. Then since your having issues with W3 Total Cache you can try HyperCache and follow the instructions to whitelist BuddyPress Pages from being cached.

    http://www.buddyboss.com/buddypress-speed-and-performance

    HyperCache should improve your Time To First Byte which is around 900ms currently. So with Hyper caching which is just PHP based, that is converting the pages to HTML, you should see a speed improvement.

    #22927

    jenni101
    Participant

    Thanks for doing that. The redirect is at server level from http site to the https site, and from what I understand I can’t change that.

    re. caching plugins that also work OK with BuddyPress, I’ve also heard of Zencache. Just out of interest – if you had to chose 1 caching plugin which one would you pick out of W3TC, Hypercache or Zencache?

    Many thanks.

    #23038

    rafaelmagic
    Participant

    Caching plugins performance can vary per Hosting. Some will perform better on your server than others. You have to install them and see which performs better.

    Try: http://gtmetrix.com/
    I think your redirect at Server level might be a Cpanel redirect. A Pure .htaccess redirect to https might be faster. Find out from your Host how they did the redirect.
    Check out your ‘Timeline” in Gtmetrix and you can see that your site is “waiting” for 1.5-2 seconds. Your DNS lookup is a little high too.
    Open a Ticket with your Host to see if they have any suggestions.

    #23051

    jenni101
    Participant

    @rafaelmagic – thanks for your help again. I’ve had a good read around things and have tested Zen Cache Lite on another site and all good. And Hyper Cache sounds excellent too, so I’ll test with both of these soon.

    re. the redirect from http to https site:
    I’ve checked everywhere in my cPanel and can’t find anything, but will double check with my hosting too. I’ve found some code options for adding to my .htaccess file and would appreciate your opinion, as well as Edward’s, as to which is best to use. I have all sites in Google webmaster tools redirecting to the www version, and the secure site certificate is registered to the www.mysite.co.nz one too.

    Option 1:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    # This checks to make sure the connection is not already HTTPS
    RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

    option 2:

    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Now, rewrite any request to the wrong domain to use www.
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    Which configuration is recommended?

    Also I think I’m getting confused between a redirect to https and ‘forcing SSL’. As from reading in this post http://forum.ait-pro.com/forums/topic/wordpress-ssl-htaccess-code-rewrite-ssl-rewritecond-server_port/#post-7233 it seems to have the same/similar code, so is a ‘redirect to my https’ site the same as ‘forcing SSL’???

    From what I’ve read here, forcing SSL can cause problems with other plugins or cause an endless redirect loop if it’s not 100% SSL, so I’m not too keen to do that. I just want to set up a redirect to it.

    Many thanks and looking forward to your comments.

    #23090

    rafaelmagic
    Participant

    Jennie,

    http://www.wpbeginner.com/wp-tutorials/how-to-add-ssl-and-https-in-wordpress/
    Double check that your websites is https: in WordPress Admin » Settings » General Settings.
    I suspect that your “Https Forward” is at Domain Registrant Level. The company that you registered your domain.
    .htaccess https forward should in theory be quicker.
    Then with caching you should be good to go.
    However for your .htaccess questions, the expert is Ed.

    #23119

    jenni101
    Participant

    Thanks again for your advice. I’ve checked all my domain settings and there’s nowhere to redirect to https there. I’ve also got my hosting company to re-check all their settings and they couldn’t find anything for a redirect to https – they thought the only thing was the https in the wordpress settings.

    So I added the redirect to https code into my htaccess and BINGO! the super long redirect from the http url has reduced to a minimum. Should have done it right at the start after I was sure the certificate was all sorted and everything was via SSL – I was just a bit nervous of this when told it could create endless redirect errors.

    So I shall continue to pursue the caching plugins and ask my host to look at their Time to First Byte too – which should speed it all up.

    Many thanks again. Cheers.

    #29601

    AITpro Admin
    Keymaster

    I have recently been testing Comet Cache: https://wordpress.org/plugins/comet-cache/ on other AITpro standard WordPress sites and am finding the speed improvement performance results very impressive and most importantly I am not running into all the problems that I have run into in the past with WPSC and W3TC on my particular web host.  I am still doing lots of testing to make sure there are no issues or problems, but so far Comet Cache appears to be a great solution and no issues or problems.  Comet Cache is currently not installed on this BuddyPress|bbPress site, but I am doing testing on a Local Dev server BuddyPress|bbPress site to make sure things are all good on that testing site.

    #29602

    AITpro Admin
    Keymaster

    I just fiddled around with this and created this code below that removes the ?ver= Query Strings and this code below has the nice added benefit of allowing you to add additional Query String parameters. You would add this code in your theme functions.php file.

    Important Note:  Before deciding to use this code read this information about why you may not want to use this code:  https://cometcache.com/kb-article/why-doesnt-comet-cache-remove-query-strings-from-static-resources/

    // Removes: ?ver= Query Strings from URLs on the frontend of a website only and not the wp-admin backend area.
    // Note: If you add other Query String parameters to the qs_array_parameters array they will also be removed.
    // Example: array( 'ver', 'example' ); would also remove this Query String: ?example=something
    function remove_ver_query_strings( $src ) {
    	$qs_array_parameters = array( 'ver' );
    	$src = esc_url( remove_query_arg( $qs_array_parameters, $src ) );
    	return $src;
    }
    
    // Remove ?ver= Query Strings on the frontend only and not wp-admin area.
    if ( ! is_admin() ) {
    	add_filter( 'style_loader_src', 'remove_ver_query_strings', 10, 2 );
    	add_filter( 'script_loader_src', 'remove_ver_query_strings', 10, 2 );
    }
Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic.