All About htaccess

htaccess techasitis

You might have came across htaccess file while hosting a website and configuring. If you have less idea and wants to know more about it means you are in right place. All you want to know about htaccess page and it’s importance is given here.  So lets get started… htaccess file is so important page in your web server, which is running Apache server software. The htaccess file is called and executed first when it is placed in a folder by Apache Server. All the rules you want to be done while handling with the content of that folder are taken care by this file. Some of the main features you can do with this file are as follow Redirects Deny any IP Address visitors Deny any Reference Visitiors Setting Server Time Zone Server Signature Access to php.ini and cgi-bin Password Protection Enable/Disable Browser Cache Enable/Disable GZIP Compression Enable/Disbale SSL Certificate and many more But here I am concentrating on last 3 points which are crucial for any website to get good Google page speed points. Google Page Speed Insight will always insists all the websites to comply these 3 rules. Try on your own responsibility. So here is, how to do that…

1.Enabling GZIP Compression

GZIP Compression method is supported by many shared hosting servers and dedicated servers, but few web hosting service providers will not provide this feature like . GZIP Compression will compress all the files which are needed to be delivered, and send it to the client browser. So that the data transfer size along with delivery time can be reduced upto 10 times. This will make your site lite and fast. You can see the difference in website speed before and after putting the below code in your htaccess file in Google Page Speed Insight testing. The servers like will not provide this feature because they think their servers will be with more work load while compressing all the files before delivering. So you just confirm before going for any web hosting service providers. So to enable GZIP Compression for your website, Go to File manager in your cPanel, and enable hidden files visibility in settings. So that all the hidden file with Dot prefix name like “.htaccess” will be visible in your File Manager. The right click on .htaccess file to Edit with Code. Before doing any changes to this file, please backup it before or copy paste in a notepad and save it for safety purpose. Test your website speed test in Google Page Speed Insight, and note down your page score. Now append the below code in your .htaccess file in your cPanel and save it. Then again test the speed and now you can see the difference in the speed of the site. Here is the code

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
<IfModule mod_deflate.c>
 # Compress HTML, CSS, JavaScript, Text, XML and fonts
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/
 AddOutputFilterByType DEFLATE application/x-font
 AddOutputFilterByType DEFLATE application/x-font-opentype
 AddOutputFilterByType DEFLATE application/x-font-otf
 AddOutputFilterByType DEFLATE application/x-font-truetype
 AddOutputFilterByType DEFLATE application/x-font-ttf
 AddOutputFilterByType DEFLATE application/x-javascript
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE font/opentype
 AddOutputFilterByType DEFLATE font/otf
 AddOutputFilterByType DEFLATE font/ttf
 AddOutputFilterByType DEFLATE image/svg+xml
 AddOutputFilterByType DEFLATE image/x-icon
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/javascript
 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/xml

 # Remove browser bugs (only needed for really old browsers)
 BrowserMatch ^Mozilla/4 gzip-only-text/html
 BrowserMatch ^Mozilla/4.0[678] no-gzip
 BrowserMatch bMSIE !no-gzip !gzip-only-text/html
 Header append Vary User-Agent

2. Enable Browser Cache with expiry time

You can enable browser cache and tell the client browser to store the common and repeated contents in it’s cache to avoid repeated requests to the server. Append the below code to enable cache

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"

3. Redirect to HTTPS from HTTP

Append the below code to redirect all the requests coming to HTTP to HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Share Your Thoughts

Your email address will not be published. Required fields are marked *