How To Increase Website Speed By Removing Query Strings From Static Resources

Website loading time is an important part of a website. It determines the user experience and the overall ranking on search engines. CSS & Javascript are static resources and are always present when you load a website. Query strings comes attached with CSS & Javascript. Query Strings are the URLs which has “&” ot “?” in them. Some servers and proxy servers are unable to cache query strings, even if a cache-control:public header is present. These strings prevent caching of CSS & Javascripts. Leveraging Browser Caching of Static Resources like CSS and JavaScript plays an important role in page loading time. In this tutorial, you will learn how you can increase the website speed by removing query strings from static resources.

How To Increase Website Speed By Removing Query Strings From Static Resources

Prerequisites

  • Domain name (example.com)
  • Admin Dashboard
  • FTP client (Filezilla, cyberduck)
  • Text Editor (Notepad, Sublime Text)

Step 1:

Login into your WordPress admin dashboard using your admin credentials. Hover over Appearance on the left side menu and click Editor.

How To Increase Website Speed By Removing Query Strings From Static Resources

Step 2:

On the right side, locate functions.php and click on it. You can also access functions.php file using the FTP client. It is located inside /public_html/wp-content/themes/theme-name. Use your favorite text editor make changes in the functions.php file.

How To Increase Website Speed By Removing Query Strings From Static Resources

Step 3:

Now, you need to add the following code into your functions.php file.

//* Remove Query String from Static Resources
function remove_css_js_ver ( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg('ver', $src );
return $src;
}
add_filter('style_loader_src','remove_css_js_ver', 10,2 );
add_filter('script_loader_src', 'remove_css_js_ver', 10, 2 );

Step 4:

There are various plugins which can help remove the query strings from static resources. Some of the popular plugins are Remove Query Strings From Static Resources, W3 Total Cache, Query Strings Remover.

  • Remove Query Strings From Static Resources: Query Strings from Static Resources can be removed using a plugin called Remove Query Strings From Static Resources. This method is specially for beginners, who don’t want to go inside the themes folder and edit the functions.php file. You need to install the plugin and activate it. There is no settings for configurations, so just activate the plugin and it will remove the strings.
  • W3 Total Cache:  W3 Total Cache plugin gives an option to remove query strings from static resources. You only need to Uncheck an option in the settings and it will remove query strings from the URLs. Go to Browser Cache tool tab and find Prevent caching of objects after settings change option. Uncheck it from General Settings and click on Save Settings button. Now Empty all Cache. Check your page loading time and see the difference.
  • Query Strings Remover: This is another WordPress plugin called Query Strings Remover. Download this plugin from WordPress plugin repository for free. Install and Activate it. There is no configuration or setting page. Activate it and you are complete. Delete all the cache and check your website on Pingdom. All the Query Strings will be removed automatically from static resources.

Conclusion:

This is how you can increase the speed of a website by removing query strings from static resources by making some changes in the functions.php  file.

Check out these top 3 WordPress hosting services:

Was this article helpful?