RSS Directory

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)


Category: Tutorials
Article posted by: Neon on July 13th, 2007



Trackback URI | Comments RSS

7 Comments to “.HTACCESS- Your New Best Friend (Part 2)” »

Comment by V. Neely
Jul 14 2007 at 9:23 pm

The “I Steal Bandwidth!” image is an especially nice touch. I imagine there are even more ways to make it more… colorful. :)

(Comments wont nest below this level)
 
Comment by sitefever
Jul 14 2007 at 9:33 pm

Yeah, I’m afraid you can get pretty creative with what do do there…

(Comments wont nest below this level)
 
Sep 19 2007 at 2:28 am

[…] 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 […]

(Comments wont nest below this level)
 
Sep 19 2007 at 4:07 am

[…] 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 […]

(Comments wont nest below this level)
 
Oct 06 2007 at 10:54 am

[…] 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 […]

(Comments wont nest below this level)
 
Oct 06 2007 at 11:38 am

[…] is the last in a three part htaccess tutorial. Feel free to review part 1 and part 2 before […]

(Comments wont nest below this level)
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.

© 2017 SiteFever.com c/o Neon.