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.
1 | Redirect 301 http://www.YourSiteName.com/old-page-url http://www.YourSiteName.com/new/page-url |
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.
1 | Options All -Indexes |
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.
1 | IndexIgnore * |
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.
1 2 3 4 5 | #To allow single extension type files IndexIgnore *.zip #To allow multiple extensions type files IndexIgnore *.zip *.jpg *.gif *.pdf |
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.
1 2 3 4 5 | ErrorDocument 400 path-of-bad-request-page ErrorDocument 401 path-of-authentication-request-page ErrorDocument 403 path-of-forbidden-page ErrorDocument 404 path-of-page-not-found-page ErrorDocument 500 path-of-server-error-page |
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.
1 2 3 | RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] |
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
1 2 3 | RewriteEngine On RewriteCond %{HTTP_HOST} www.YourSiteName.com RewriteRule (.*) http://yourSiteName.com/$1 [R=301,L] |
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.
1 2 3 | RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.YourSiteEName.com/$1 [R,L] |
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.
1 2 3 | <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$"> Header set Cache-Control "max-age=2592000" </FilesMatch> |
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.
1 2 3 4 5 6 7 8 9 10 | <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> |