7 Useful Htaccess Rules & Tricks

7 Most Useful .htaccess Tricks

What is .htaccess File and How to Edit or create it?

The .htaccess file is a powerful server configuration file that provides multiple commands for controlling and configuring the Apache Web Server. It is located in your site’s root folder. You need to connect to your website using an FTP client to edit it or you can directly edit from File Manger by using cPanel access. In most cases, it’ll be there on your server, but it doesn’t, don’t worry. You can open any HTML editor or notepad to create an HTACCESS file. You have to write the required rules and save the file name with “.htaccess” (without double quotes) and need to upload it to the root directory of your website.

Important Note: Before editing your .htaccess file, it is suggested to download a copy of the existing file to the safe place of your computer as a backup. You can use that file in case anything goes wrong and the site not working as desired. So, in this case, you can easily revert back the file and keep your site working as it was.

What is the importance of the .htaccess file?

The .htaccess file not only for speeding up your site but also for SEO optimization, protect your webserver against malicious attacks and a number of other purposes.

In this article, I will show you some of the most useful .htaccess tricks for your site which should be there for better SEO ranking and site performance.

1. Setting up 301 Redirects:

If any of your website’s page URL somehow change, you have to use 301 redirects to let google know that URL has been changed and need the user to redirect new URL instead of the old ones.

2. Disable Directory Browsing:

Security experts recommend disabling directory browsing. If your site has directory browsing enabled, hackers can easily look into your site’s directory and file structure to find vulnerable files to damage.

There are two options to protect your website directory

First Option: Restrict complete directory for the users, so nothing can be displayed on his screen and show the message “Forbidden” instead.

Second Option: Allow directory browsing but disable file listing. While using this rule, the user can see the directory structure but not any files saved within.

The “*” in the above code represent a wildcard, it means it’ll not display any files. If you want to list or display certain types of files like .zip, .jpg, .gif, .pdf, etc to your customer to download or access, you have to use the following command to mention the allow the file types to be shown.

3. Custom Error Pages

When a visitor lands to any non-existing website page, the site will show the default error pages which are not properly formatted with a custom message. So, to give a proper message and browsing experience it’s good practice to use custom error pages with a custom message for visitors. You can use the following rules for different types of browsing errors to deal with.

4. Redirect Non-www to www URLs

Redirecting non-www requests to the www version of your website is beneficial to SEO. It helps your site to avoid duplicate content on the Google index. If your website runs to non-www and www both way the rank of the page also be different for both types of URLs.

If you don’t want to add “www” to your website URL and wants to run the site without it, use following rues to Redirect www to non-www URLs

5. HTTP to https redirect

Your site should be run on a secure URL for a better Google rank. Security is the top priority for Google. If you have SSL installed to your server, it’s suggested to redirect visitors to https URL instead of normal. HTTPS is the most important part of Google SEO, so don’t take it as normal and take it in high priority.

Don’t worry about the cost of SSL, many shared hosting offers free SSL and you can add your site to the Cloudflare to get free SSL if your hosting provider doesn’t offer it free.

6. Cache Files

If your site page load faster it’ll get a good rank in search results. File caching helps to optimize website loading time and performance.

Note: default cache 1 year = 31556926 s

7. GZip Compress Files

To load your webpage faster, as the “Cache file” rule is playing an important part, this rule also helps to optimize your website loading time by compressing files into a smaller size. The compression of HTML and CSS files is quite popular now as it provides for faster page loading.

Leave a Reply

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