Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Code Newbie > Submit Tutorials > HTML / CSS

Reply
 
LinkBack Thread Tools Display Modes
Old 08-27-2004, 04:43 AM   #1 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
.htaccess -- Image Hotlinking Prevention

I’m not going to put loads of beginner rubbish in here because if you have gotten as far as wanting to use your own custom pages then you’ll know what I’m talking about in this article.

A file called .htaccess placed in certain directories of a server can have many affects such as:
  • custom error pages
  • image hotlinking prevention
  • directory listing prevention
  • redirects
  • enabling SSI
  • deny users by IP
  • password protection
  • setting default directory
  • adding MIME types
  • prevent viewing of htaccess by users

One of the more popular ones used is the image hotlinking prevention bullet.

What do I need?
Your server to support .htaccess files. If somebody else runs the server and they don’t work then try emailing them asking them what they can do about it. If they are a free host then they won’t have .htaccess and won’t listen to your email. If you are running your own server and your .htaccess file does not work then you need to refer to your server’s documentation to set your server to allow usage of .htaccess files.

Your server also needs to support mod_rewrite. It probably will, but if this technique doesn’t work then you should try contacting the host admin and ask if they can enable it or something.

A text editor. The simpler the better. Winblows users just crack open that editing dynamo – Notepad.exe
Any developers that use simple text editors like Notepad can be proud =]
The best reason for this is other word processors will probably throw in extra characters and formatting that will throw your Apache server into seizures with errors foaming from it’s broadband ports … not a pretty site! (literally)
If you insist on using Word 2003 then disable word wrapping and make sure you save the file as .txt

How do I save this file.htaccess ?
Definitely not like that! .htaccess is a file that disobeys every file naming convention you can think of. It is it’s own file extension!

After you type in your .htaccess syntax as described below, save the file as ‘htaccess.txt’. Make sure you have the option enabled in your operating system that lets you view (and rename) file extensions as well as file names. Go to the htaccess.txt file and rename it. Take the ‘.txt’ bit off and add the dot ‘.’ To the beginning of the name to give you ‘.htaccess’.

Notice how the file is now in an unknown file format? Don’t try and open that file up by double clicking it – just open Notepad again and go to File > Open to open it manually.

But it already exists on my server!
Then all you need to do is edit it. The .htaccess file is strict about white space and carriage returns so do everything on one line. Just either edit the line if it exists already or add it to the lines. The .htaccess lines are in no particular order unlike files such as .HTML.

Can I have more than one file/setting for different folders?
Yes! If for instance if a main site such as ‘www.England.com’ already has a .htaccess file that does things to its folders that you don’t like then just add another .htaccess file into the folder like ‘www.England.com/London’ then these will over ride any lines in the main site.

Confused? Well imagine living next door to an off-license and having a supermarket a mile down the road. If you wanted a bottle of wine then you wouldn’t go all the way to the supermarket would you? No you’d go next door! But imagine you wanted to buy some deodorant. Next door does not sell it so you would go to the next nearest place being that supermarket!

If the nearest .htaccess file does not specify a line, then the next nearest will be referred to and so on.

Disabling image hotlinking
One of the biggest gripes for web hosts and their admin is the theft of their bandwidth (and in turn, money) from hotlinking from other sites. Instead of referencing their images to their own servers, they link straight off somebody else’s without having to pay the bandwidth cost. This can disabled with the following script:
Quote:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mywebbie.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ - [F]
Remembering each command goes on a single line. Replace the ‘mywebbie.com’ bit to your own domain.
But it doesn’t just stop there! Oh no! How about we get our own back and get some free advertising on the way? Just change the last line of the above script and add this line instead:
Quote:
RewriteRule \.(gif|jpg|png)$ http://www.mywebbie.com/thief.gif [R,L]
Remembering each command goes on a single line. Just take the thief part out and replace with relevant directory route link to the image you want to replace on their pages! For the free advertising bit, just put a small advert of your choice in there and include your web address – you can’t surround the image with a <a href=””></a>.

This is because whenever a file with the extensions jpeg, gif or png is requested from outside your website, it is replaced with the set image. The one in the above example is thief.png .

I’ve made my .htaccess file, now what?
Now you need to upload the file to your server. You should have really planned your htaccess beforehand if you wanted different things to happen to different folders but for default’s sake, upload this to your site’s root folder. When you FTP the file, make sure you do it by ASCII. This is very important! I’m not sure why but it does some screwy things to the file if you upload using binary.

I just can’t rename my file to ‘.htaccess’
If your OS won’t let you rename your file to .htaccess then try uploading the file to your server before trying to change the name using the FTP program.
If you run your own server or somebody else is running a server dedicated to you then you can change the configuration of the Apache software to recognise the .htaccess by another name. Simply go into the Apache directories and open up the configuration .config file. Look for the line that reads,
Code:
AccessFileName .htaccess
Remember to restart Apache after config file changes.
Edit it according to the Apache manual.
http://httpd.apache.org/docs/mod/cor...accessfilename

How do I set Apache to read .htaccess files?
If the server does not work on these files as if they never existed then that is probably because they are simply being ignored. Go into Apache’s config file again and look for the following line and modify it according to the below example.
Code:
AllowOverride All
Remember to restart Apache after config file changes.
The options for setting the AllowOverride directive can be found at the Apache manual.
http://httpd.apache.org/docs/mod/cor...#allowoverride

Article version 1.0
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
DavH27 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Small modification needed on image flipping JavaScript Epsilon HTML, XML, Javascript, AJAX 0 08-31-2004 06:06 PM
Server config / .htaccess DavH27 Lounge 12 08-26-2004 06:16 PM
Finding the size of an image? Epsilon PHP 2 06-22-2003 03:23 PM
I am a newb, how do you set multiple parts of an image as a link? I sniper baby HTML, XML, Javascript, AJAX 5 02-10-2003 07:40 PM


All times are GMT -8. The time now is 03:08 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting