Search The Hostwinds Guides Knowledge Base

Optimize Website Using .htaccess File

Having your website loading fast is a passion for most Hostwinds clients and we know how important this is. This guide is to help with reviewing your website and it’s load speed so that optimization can be seen and tested by you or your website developer.

We will also explore many options for you to use and common ways to Optimize your website using a .htaccess file. This is intended for shared hosting, business hosting, cPanel clients and VPS, Cloud and Dedicated Server clients who are using Apache web server.

For this guide, we will start out by using Google Chrome’s built in tools and using .htaccess for an Apache web server.

 

Create .htaccess File

The first thing to do is to create your .htaccess file. Before continuing with this guide, it is important to make a backup of your file to ensure this file can be restored in case your website does not load or you have any issues.

 

Google Chrome DevTools

#1 Visit your website and launch Google Dev Tools with F12 on Windows or also Ctrl + Shift + I on your keyboard. If you are using a Mac, you can use Cmd + Opt + I. Or, just right click on the web page and select Inspect Element to click on the Network tab

#2 Check the box in disable cache

#3 Click on Network from this same menu and reload the page. Here, the results and load time will show at the bottom of the screen

dev tools load time

Refresh the web page and take note of the load time as shown in the image above. You will also want to take note of the Time To First Byte which would be ideal to be under 200 ms.  The image below shows this as being 120 ms as an example:

time to first byte ttfb

 

Website Optimization

Utilize Caching

Compress Content Using Mod Deflate

Gzip Compression

Caching

Force Trailing Slash

Prevent Hotlinking

Replace yourdomainname with your actual domain name URL and hotlinksnotpermitted.jpg

Redirect Website Visitors to a Mobile Site:

Stop SPAM On WordPress Blog

Replace yourdomainname with your actual domain name URL

Redirect Feeds

Replace yourdomainname with your actual domain name URL

Configure Your Website for HTML5 Videos

Log PHP Errors Into An Error Log

Replace yourdomainname with your actual domain name URL and ~/public_html/php_error.log with the location of your log file

Run PHP Inside JavaScript Files

Block User IPs

Replace 123.45.6.7 and 0.1.2.3 with the actual IP addresses you want to block

Prevent Directory Listing of Files

Custom Error Document

Replace /directory/filename.ext with the error document you would like to use and /errors/lostandfound.html with the 404 error page you would like to use to replace the default error documents

Redirect Non WWW URL to WWW URL

Replace yourdomainname in three places, /olddirectory/oldfile.html with the old file name and newfile.html with the new file name

Simple Redirect

Replace /olddirectory/oldfile.html and https://yourdomainname.com/newfile.html

Redirect Website to https 

Replace yourdomain.tld with your actual domain name

 

Related Resources