How to Optimize .htaccess of a WordPress Website

You are here : Home / How to Optimize .htaccess of a WordPress Website

The .htaccess (Hypertext Access) is a very powerful file. It allows you to make changes in Apache web server configuration settings without affecting the main configuration file. In other words, you can modify the way the server behaves without changing the core settings. It is similar to using a child theme in WordPress because when we edit in child theme it does not effect on the parent theme. It is often used to specify the security restrictions for the particular directory. And an optimize .htacess file can increase speed and security of your WordPress site.

Be Careful while Optimizing .htacess

Before you optimize or modify .htacess file you must have a backup of your .htaccess file. If something gets spoiled, you can change the hacked .htaccess with the original one. On opening .htaccess file you can see some codes are already present in file.  But don’t touch these codes because by default these codes are required by permalink structures. Erasing these codes may cause major issues with your website. Also .htaccess has the ability to lock you out of your domain so be careful while changing in .htaccess file. Tricks for optimize your .htaccess are given below:-

Optimize WP’s 404 Response

WordPress has a 404.php template that’s created for the posts and pages that does not found in the website. but if post/page contains an image that’s deleted or have a incorrect path then It will show 404.php template to the browser but it  doubles the traffic for a single page because for every not found object it will multiply traffic/load for every page. Also, some robot’s request some odd urls that are not existed on your site that also slows down the server. So updated .htaccess file will look like that:

Gzip File Compression

Compression reduces response times by reducing the size of a file. But keep in mind that it is not good to compress small size files because as a result of compression below the size of 500 kb will definitely increase the loading time. Image and PDF files shouldn’t be gzipped because of they are already compressed. And if we compress them again then it not only wastes processor but also increase the file sizes. It’s valuable to gzip your HTML documents, scripts and stylesheets. By adding this code into your file you are able to use gzip compression.

Turn-off Directory Indexing

By default, it is possible for every visitor to look within any of your website directory that does not contain index file like index.html, index.php, etc. in it. Which means configuration files and different sensitive data may be misused by nasty users. So the only solution to prevent that we have to add a blank index.html file to each folder on your web site. but if you have a large number of directories then  it is time consuming work so better option is to just open and change your .htaccess file by adding following code into your file. and it will solve this problem.

Prevent Hotlinking

Hotlinking is the act by which we display an image from another website via URL. By this we can save a lot of space because images are not stored in our hard disk but don’t worry it’s possible to prevent other domains from hot linking to your website. But on loading webpage your website request for the image and this process eats up bandwidth on the host’s server. And slow down you page loading.

To make sure no one is using your valuable bandwidth, add this script to your .htaccess file

Disallow Spam Robots

Your web site is frequently crawled by robots. Some of these robots are necessary like search engine robots because they index your site so it will show in search engine results. But on the other hand some robots are not so friendly .So these robots are called Spam robots and they just jammed your server by using up bandwidth and resources. We can block robots based on the user-agent they provide.

The script below denies some spam robots, but isn’t exhaustive.

Implement a 301 Redirect with .htaccess

If you have forever changed the URL structure on your site because of optimization change or CMS migration and you will want to implement 301 redirects from the older URL to the new URL.The syntax for a basic 301 redirect is given below need to add in the .htaccess file. The first URL should be a relative path to the older URL and the second one should be an absolute path to the new URL.

Protect .htaccess itself !

It is important thing to protecting your .htaccess file, because it contains some real important secrets, so we can’t leave the file itself open to attack. The following code can help .htaccess file from the hackers.

Better still, you can rename the .htaccess to any other name you like

 How to Optimize .htaccess of a Wordpress Website

Flipper Code

We have been building WordPress Plugins at flipper code since 2008. We follow wordpress coding standard that ensures we deliver the excellent wordpress plugins and services.

More Posts - Website

Follow Me:
twitter How to Optimize .htaccess of a Wordpress Websitefacebook How to Optimize .htaccess of a Wordpress Websitegoogleplus How to Optimize .htaccess of a Wordpress Website

Customize Product according to your requirements?

We will be glad to discuss any of your needs related to our wordpress plugins . if any of functionality you'd like to see in products, you can reach us by via email hello@flippercode.com.