.htaccess noob questions

They have: 11 posts

Joined: Apr 2004

My objective is this: every file sent out by the server with a .gif extension should be run through a php script first. E.g., if you request mydomain.com/example.gif, I want the server to send you script.php?url=example.gif. Simple enough?

I presume there's a way to do this using a .htaccess file. (I'm running Apache 2). I figured I'd just try it out, by inputting this script I pulled off of some website that's supposed to prevent remote image loading:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^[url]http://booya\.no-ip\.org/[/url] [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpe?g|gif|bmp|png)$ images/nohotlink.jpg [L]
'

Here I encountered my first problem: I can't name the file .htaccess because Windows won't allow a filename to begin with a period. Does the filename have to be ".htaccess", or can it be something like "something.htaccess"?

I've tried naming it "something.htaccess" and it doesn't seem to be blocking anything.

Of course, my second question once this is working is going to be, how do I get it to do what I want it to do? If someone just wants to link me to a writeup on how this syntax works, that would be appreciated.

s0da's picture

He has: 157 posts

Joined: Mar 2004

trying the internal program in windows.

C:\> rename something.htaccess .htaccess

They have: 11 posts

Joined: Apr 2004

excellent, thank ye...

test image:

Yet, still doesn't work...

s0da's picture

He has: 157 posts

Joined: Mar 2004

does your server use cpanel? if so then you can do it through there instead of doing it yourself.

http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=htaccess+image

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

Cpanel is not currently available for Windows Servers. You should be able to rename the file by doing this from the command line (DOS prompt).
ren something.htaccess ".htaccess" with the quotes around the .htaccess. You usually don't have that problem in Windows 2000/XP, mainly on Windows 9X machines.

Doing something like that will be very processor intensive on your site. Just curious what your reason is for attempting this?

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

They have: 11 posts

Joined: Apr 2004

Yep, I have the file renamed fine... funny, I'm running Windows 2000.

But it doesn't seem to do anything.

For the record, I'm just building the site on my Windows box. The server where it will end up will undoubtedly be running a real OS.

The php script embeds text into a gif file. Since many people don't know about gif comment fields, this should serve as a decent security mechanism to prevent people from ripping off our images.

s0da's picture

He has: 157 posts

Joined: Mar 2004

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://booya.no-ip.org:8080(/)?.*$ [NC]
RewriteRule .*\.(gif|jpg|jpeg|png|bmp)$ images/nohotlink.jpg [L,F,NC]
'

They have: 11 posts

Joined: Apr 2004

Still no...

It looks like Apache isn't even looking at .htaccess. It's in the same directory with the image... do I need to do anything with Apache's config files?

s0da's picture

He has: 157 posts

Joined: Mar 2004

maybe its the :8080 try that

They have: 11 posts

Joined: Apr 2004

Still... not... doing... anything.

s0da's picture

He has: 157 posts

Joined: Mar 2004

is your mod_rewrite being loaded in your apache config?

They have: 11 posts

Joined: Apr 2004

I... have no idea what that means.

Update:
I got this to work by changing the config from "AllowOverride none" to "AllowOverride All".
I've also uncommented LoadModule rewrite_module modules/mod_rewrite.so

Now I just don't know how to get it to do what I want. Can anyone help?

I need to replace (foo).gif with secure.php?url=(foo).gif. A few issues: since there can't be slashes after the question mark in the url, I need to escape those or replace them with another character; also, it needs to only do this replacement if ".php" isn't in the url, to prevent looping.

I don't understand the syntax well enough at this point to write anything that works... so any help would be great.

They have: 11 posts

Joined: Apr 2004

Mmmkay, I got it working:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*).gif$
ReWriteRule ^(.*)?/(.*)?$ $1~$2
ReWriteRule ^(.*).gif$ secure.php?url=$1.gif
'

Problem solved.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.