Optimize .htaccess codes and increase your website performance

0 354

Apache .htaccess file is the main file which helps your website or apache server to boost performance, Optimizing and tweaking the .htaccess data can help your website to increase the website performance. I have compiled the best .htaccess file codes for you to copy and paste the codes in the root of the website directory. The root directory for cpanel is the Home directory where you need to place the codes inside your .htaccess file.

The .htaccess file is hidden, so you need to ask your hosting provider to direct on how to show .htaccess file. For Cpanel, you can log in to Cpanel then navigate to File Manager and later on the right side top you will find a setting icon or text click on it and check mark ” Show hidden Files ” that’s it now upon saving it will show the .htaccess code to you inside your file manager.

.htaccess codes generally present inside the public_html folder and outside the public_html folder or any other directory where it is possible and each app like WordPress, Joomla have their own set of .htaccess file.

Speed up WordPress site - htaccess optimization WordPress

How .htaccess file works?

I have researched a lot and found that .htaccess is the heart of the Apache server. .htaccess file can also be mentioned as“Directives” for the website to work. It is mainly used for doing several works like redirecting the webpage from one page to another. Increasing the file upload speed or file upload size or Memory execution power. However, these require a short command for each function to include in the .htaccess file.

An .htaccess file also allows you to execute a set of these directives command for making the changes in the website action without the need of changing  Apache’s core server configuration file named as https.conf.

How .htaccess can be used?

  • It will help to Optimize your Website
  • It can be used to improve page load time
  • It can create a redirection of pages from or to
  • You can use it to create password protected folders or directories
  • You can set .htaccess to block specific IP or all the ips except your ip.

However, we have understood a lot for the .htaccess file now going straight to the codes that we need to put inside the .htaccess file for making our website faster and caching at the visitor’s browser so that our website would serve faster to the visitors and decrease the bandwidth and CPU usage.

.htaccess codes for optimizing website performance

.htaccess codes

Copy the below codes to your existing or create a new .htaccess file if no .htaccess file is present in the root directory . These codes will be put inside Public_html Folder.

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
# BEGIN Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 5 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(css)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(js)$">
    Header set Cache-Control "private"
  </filesMatch>
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "private, must-revalidate"
  </filesMatch>
</ifModule>
# END Cache-Control Headers
# END Expire headers
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
ServerSignature Off
Options -Indexes

Okay now, let me explain them in detailed about what you are copying and how they will work for your website.

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

The above codes will make your website GZip compression for your site which you can check here: https://checkgzipcompression.com/

# BEGIN Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 5 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
# END Expire headers

Okay so above code will help the visitor’s browser to cache the files like jpeg, gif, png, CSS, javascript or HTML file to cache on their browser so on the next visit, they won’t need many requests from your server, and it will load the webpage instantly.

Example :

1st Request

Visitor’s Browser > Requested the Page > Page Data like HTML, image files and CSS also javascript is being loaded.

3 Seconds load time.

Then visitor navigated to another page.

2nd Request

Visitor’s Browser > Requested extra images > Preloaded the CSS and javascript also the image instantly from browser saved cache > Extra images, CSS and javascript are being loaded.

2 Seconds load time.

3rd Request

Visitor’s Browser > Requested to visit Home Page > Loads all the data from browser’s saved data and cache > No Extra Request > Loaded instantly.

0.1 Seconds load time.

# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(css)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(js)$">
    Header set Cache-Control "private"
  </filesMatch>
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "private, must-revalidate"
  </filesMatch>
</ifModule>
# END Cache-Control Headers

Okay, now the above code will help the website to mention the browser how to cache the website in the visitor’s browser and direct the files to be set in the browser.

<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>

The above codes will disable the access to the visitors from access any .htaccess file. So it means it is adding the security to your website so that none of the visitors can access and read the file except you.

<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>

Now to help the apache heart always serve the website to the visitors and make the performance faster it is must require that you enable the keep-alive function, so I have added this to improve the flow of data to the visitors without waiting for the new connection.

ServerSignature Off

So no one loves to keep any trace of the server hence disabling the server details is best so this will help to protect your server to provide additional information to the hacker.

Options -Indexes

Why let the visitors to browse your empty folders? This will help the visitors or hacker to gain access to your website’s data by manipulating the folder, and this provides a security risk so to stop the visitors from browsing any empty folder the above command will protect by placing a blank index.php file inside the empty folder.

If you are having a further discussion do love to comment below and I will be highly appreciated to discuss the further development of this article and help the website owner to increase the website performance.

Leave A Reply

Your email address will not be published.