Now that you’ve got your htaccess file created, you’re ready to start using it. Believe it or not, your htaccess file is used for more than just error pages, and I’m going to show you some of what you can do.
If you didn’t catch part 1, which is an introduction to htaccess, read that post first.
Block Users by IP
If you notice that somebody is coming to your site and trying to do something you don’t like, just ban them! This is especially useful if you have a website where users can contribute to the content of the site, such as with a forum or blog, and somebody is spamming you.
So, open up your .htaccess file and type the following:
order allow,deny
deny from 595.33.8.7
deny from 012.34.5.
allow from all
Of course, you would replace the IP’s with the ones you wish to block. You can either type in the entire IP address to block that particular IP, or you can block groups of IP addresses.
For instance, in the above example, deny from 012.34.5 will block all IP’s under that one, such as:
012.34.5.1
012.34.5.2
012.34.5.3, etc.
Deny Referrer
You can also deny a visitor if they are coming from a particular referrer. A new tactic used by people to hurt your search engine rankings is to send a lot of bad traffic your way. For example, if you rank #1 in Google for the search term “cool blog” and I’m #2, I might spend $50 to send 500,000 fake garbage bot visitors to your site hoping that Google will recognize your traffic is not all genuine and bump your position down a little, giving me the #1 spot. In such a case, you would see thousands and thousands of hits coming from the same referring URL in your site stats, usually coming from a cheap traffic company.
Note: Your hosting provider must have mod_rewrite enabled on your server in order for this code to work.
Here’s the code:
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} trash\.com [NC]
RewriteRule .* - [F]
Replace “trash” with the domain and “.com” with any other extension the site may be (.net, .info, etc.)
To block more than one referring URL, you would use:
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} trash\.com [NC,OR]
RewriteCond %{HTTP_REFERER} moretrash\.com
RewriteRule .* - [F]
The only difference here is that you would add “[NC,OR]” after each domain listed except for the last one in the list.
If you get a 500 Internal Server Error when using this code, just delete the “#” in the second line of code.
Prevent Hotlinking
Somebody stealing your bandwidth? If you have a picture or video on your website that people like, sometimes they may post it on their websites, but it will be pulling everything off of your website using your bandwidth.
Again, you will need mod_rewrite to be enabled by your web host.
Here’s how to do this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|js|css)$ - [F]
Of course, you would replace “yourdomain.com” in the code above with your actual domain name. You would also enter all the domain extensions you would like to disallow hotlinking to in the last line. The code above is set to disallow hotlinking any gif, jpg, js or css files from your site.
To really have fun, you can show whatever content you want when somebody tries to hotlink from your site. For example, if somebody tries to hotlink a pretty image of a sunset on the beach from your site, an image saying “I STEAL BANDWIDTH!” can be shown.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.yoursite.com/yousteal.jpg [R,L]
In the code above, if somebody tries to hotlink a gif or jpg image from your site, it will display a picture called yousteal.jpg on their site!
I’ve got some more tricks up my sleeve for you regarding htaccess, but I’ll have to include them in my next post on this series, part 3.
If I make these posts too long, I’ll get a ton of angry emails…
Check back soon for .HTACCESS- Your New Best Friend (Part 3)
Enjoyed this article?Subscribe to SiteFever.com's Full Feed RSS to get daily updates of this blog!
Be a good sport and promote my article! - del.icio.us - Furl - Netscape - reddit - StumbleUpon





Add your comment!
(3 votes, average: 4.33 out of 5)


The “I Steal Bandwidth!” image is an especially nice touch. I imagine there are even more ways to make it more… colorful.
Reply to this comment
Yeah, I’m afraid you can get pretty creative with what do do there…
Reply to this comment
[…] they’re doing (in so many words). It will also be a great list of IP addresses for us all to add to our .htaccess file to ban them from our blogs […]
Reply to this comment
[…] they’re doing (in so many words). It will also be a great list of IP addresses for us all to add to our .htaccess file to ban them from our blogs […]
Reply to this comment
[…] is the end of part 1 of our .htaccess tutorial. Part 2 will walk you through some of the functionality of your .htaccess file. Add your comment! Enjoyed […]
Reply to this comment
[…] is the last in a three part htaccess tutorial. Feel free to review part 1 and part 2 before […]
Reply to this comment