Showing posts with label htaccess. Show all posts
Showing posts with label htaccess. Show all posts

10 May 2011

301 redirect non www urls to www using htaccess

In the search engine optimization url canonicalization issues are very common. What is url caonicalization? Basically url canonicalization means that two or more of your urls are having duplicate content. An example of such urls is as follows:


http://example.com
http://www.example.com

Both of the above mentioned urls will have the same content therefore canonicalizing each other. To resolve this issue we 301 redirect one of the url to other. Mostly we 301 redirect non www url to the one with www.

This can be done simply by adding the following code in the .htaccess file:


###########FOR example.com to www.example.com###########
RewriteEngine on
Rewritecond %{http_host} ^example.com [nc]
Rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
#########################################################

For any queries or questions, feel free to comment.

01 January 2011

How to stop image hotlinking using htaccess

Sometimes it does happen that people use your images using the link of your site. This uses your bandwidth without coming into your notice. In order to prevent this image hotlinking I am sharing a very simple and easy to use code. All you need to do is place the below mention code in your htaccess file and say bye bye to all your worries regarding image hotlinking.
##############STOP HOTLINKING############
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?your-domain\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]
#########################################

For any queries and problems feel free to comment and don’t forget to replace “your-domain” with the name of your domain in the above code.

31 December 2010

How to block an IP using htaccess

Sometimes you need to block an IP which is doing DOS (Denial Of Service) or DDOS (Distributed Denial Of Service) attack on your site. For this purpose I am sharing a simple htaccess code. You just need to place this code in your htaccess file and the attacking IP will be blocked from your site.
##########BLOCKING AN IP################
< Limit GET HEAD POST>
order allow,deny
deny from XXX.XXX.XXX.XXX
allow from all
< /LIMIT>
########################################

For any queries and problems feel free to comment and don’t forget to replace “XXX.XXX.XXX.XXX” with the IP address that you want to block in the above code.