Redirecting Image Files to Parent Directory with .htaccess

cziffra

New member
Jun 24, 2006
421
19
0
Somalia
I need to redirect image files to their parent directory with .htaccess. I'm trying to prevent direct access to images for a membership site. I don't care if someone can save the file. I just need to block direct access to images so I can use PHP to display a preview image to non-members instead of the full image that members will be able to access. For example, if someone accesses this URL:

Code:
http://example.com/folder/image.gif

I want to redirect the visitor to:

Code:
http://example.com/folder/

Note that 'folder' could be any path with varying levels of subdirectories, so I need this to work for any URL if possible and also allow the image to be displayed on a web page. Standard hotlink protection won't really work for this situation. I've searched for a half hour or so, but haven't been able to find a solution. Everything that comes up is either related to hotlink protection or is irrelevant. Does anyone have any suggestions? Thanks!
 


That's pretty easy, just copy and paste the title of this thread into the fucking Google :) Hope that helps!
 
That's pretty easy, just copy and paste the title of this thread into the fucking Google :) Hope that helps!

Have you ever actually contributed any value to this forum? It's ironic that you are too lazy to read my post and then tell me to search when I specifically mentioned that I had already searched for an answer. I spent another hour or so searching last night. I couldn't find anything on this specific situation. I didn't post my question because I'm too lazy to search.

You are now on ignore.
 
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder/(.*).(gif|png|jpg|jpeg|tif|bmp)$ http://example.com/folder/ [R=301,L]
 
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder/(.*).(gif|png|jpg|jpeg|tif|bmp)$ http://example.com/folder/ [R=301,L]

Thanks, but this isn't quite what I'm trying to do. The images will be in different directories, so I want to be able to redirect any image to its parent directory. There will be too many different directories to add each one to the code manually. This means that the redirect would need to handle different directory names without them being specified. I'm also trying to do this in a way where the image will still display on a web page but not allow direct access to the image. I'm not even sure if this is possible. If I have to, I could probably figure out some way of redirecting from a custom 403 page, but I prefer to try it with .htaccess first if possible.
 
why not just check for a session cookie
 
why not just check for a session cookie

If you're talking about what I think you are, I don't think it would work. I want to display different content on a web page depending on the visitor's membership level or if they aren't signed in. I need to prevent direct image access to do this properly. It's unlikely that most non-subscribers would even know how to access the full image directly, but I want to at least try to make it difficult to get the paid content without paying. I would prefer to achieve this with a redirect so I can use PHP to handle what is displayed to the visitor. If I use a redirect, the preview image can be displayed with information about signing up to see the full version. The visitor would also have a very difficult time accessing the paid content without subscribing since the redirect would block direct access and the PHP page would only display the preview image.
 
Code:
RewriteEngine On
RewriteRule ^(.*)/([^/]*)\.(gif|png|jpg|jpeg|tif|bmp)$  $1 [R=301,L]

I tested this and it didn't work. The redirected URL included the root path for the server (e.g., /home/username/public_html/rest-of-path ). The image also doesn't display within the web page. Thanks for trying, though.
 
Not sure I get what you want. This does exactly what you asked for in the original post. If it's including /home/blah/whatever, that's some interaction with some other configuration you have. On a vanilla server, it just redirects http://site.com/whatever/path/some.jpg to http://site.com/whatever/path/

It's not going to display an image, because it's redirecting them back to the folder, which isn't an image.

What if a folder has more than one image? Are you expecting, for example:

http://site.com/folder/bird.jpg and http://site.com/folder/dog.jpg to both redirect back to http://site.com/folder and somehow display unique images each time, even though the url is the same?

If that's the case, .htaccess isn't going to work for you. You'll need a script of some sort that captures the referer and dynamically pumps out an image with the correct MIME type.


I tested this and it didn't work. The redirected URL included the root path for the server (e.g., /home/username/public_html/rest-of-path ). The image also doesn't display within the web page. Thanks for trying, though.
 
Not sure I get what you want.

I need to do two things only:

1. Redirect any image to its parent directory when the image is accessed directly.

2. Allow the image to still display within a web page on my site.

When the script redirects to the parent directory everything else will be handled by my script. As long as I can do the two things above it doesn't matter how many images. My PHP code will handle which image to display.