You have heard about it but never used it. Yes, I am talking about the powerful .htaccess file. It can not only enhance the performance of your website but also enhance the security.
Here are few simple but powerful codes to be used with .htaccess file to enhance the performance of your website considerably. Before you read on further, go to the root directory of your site which is most probably ‘public_html’. Find .htaccess file in it. If you do not see the file, contact your host to verify if they give you access to the .htaccess file. If you are on economy shared hosting, there are very low chances of you getting the .htaccess file.
Note: **All commands in .htaccess file should be a new line. You can write comments by putting #before a sentence. Once you get the .htaccess file, you’re ready to use it.
To redirect users to your preferred domain prefix (xyz.com to www.xyz.com or vice versa)
Simply copy paste the following code and replace domain name with your domain in it.
RewriteEngine On
#Redirect Rules
RewriteCond %{HTTP_HOST} ^xyz\.com$
RewriteRule (.*) http://www.xyz.com/$1 [R=301,L]
The above code will simply redirect users to www.xyz.com if they type in only xyz.com in the browser. Isn’t it great!
To disallow any values to be send to your web server except GET method from another domain
RewriteCond %{REQUEST_METHOD} !^(GET)$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?xyz.com/.*$ [NC]
RewriteRule .* - [F,L]
The above code will not entertain any values send to your web server from another domain. This is very effective to stop hackers from sending their data from a local machine.
To enable caching for faster & friendly user experience. It helps to load pages much faster than turned off
This feature enables you to allow your resources to be cached by users’ browser, ultimately leading to faster page loading. This can increase your web page loading almost by 50%. Here you need to define a time for which you want the browser to cache resources. It is defined in ‘no of seconds’ from the time of loading it from the server. If you want to allow caching for 7 days, it would be 7*24*60*60 = 604800
#For allowing caching of .js files
<Files *.js>
Header add "Cache-Control" "max-age=604800"
</Files>
#For allowing caching of .jpg files
<Files *.jpg>
Header add "Cache-Control" "max-age=604800"
</Files>
#For allowing caching of .png files
<Files *.png>
Header add "Cache-Control" "max-age=604800"
</Files>
#For allowing caching of .gif files
<Files *.gif>
Header add "Cache-Control" "max-age=604800"
</Files>
#For allowing caching of .css files
<Files *.css>
Header add "Cache-Control" "max-age=604800"
</Files>
To disallow any particular ip address to access your site
Yes, there are sometimes when you don’t want to allow a particular visitor to your site or even a bot.
#To deny access to particular ip address
order allow,deny
deny from 255.0.0.0
allow from all
#To deny access to multiple ip address
order allow,deny
deny from 255.0.0.0
deny from 127.0.0.1
deny from xxx.xx.x.x
deny from yyy.yy.yy.yy
allow from all
#To deny access to an ip address range
order allow,deny
deny from 255.0.0
allow from all
Denying 255.0.0 will deny any ip address starting with 255.0.0