Welcome to WebmasterWorld Guest from 23.22.17.192

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite URL

   
7:53 pm on Feb 4, 2013 (gmt 0)

10+ Year Member



I am trying to redirect hotlinked images to the image page with .htaccess. The format of the image is:
http://www.mysite.com/images/photos/dog-IMG1234_file4.JPG


I want to capture this and rewrite it to be this:
http://www.mysite.com/cgi-bin/photo.cgi?dog-IMG1234


I know how to redirect to my main page but I dont know how to split the image url up to get out the dog-IMG1234 part. Can someone help me please?
8:34 pm on Feb 4, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



What code have you tried so far?

A rewrite involves an external URL (with hostname) and an internal filepath (without hostname). Which one is which here?

You have quoted two URLs.
9:07 pm on Feb 4, 2013 (gmt 0)

10+ Year Member



The 1st URL is an image file. The 2nd is a webpage with the photo on it.
9:19 pm on Feb 4, 2013 (gmt 0)

10+ Year Member



This is basically what I have so far, I want to redirect not to my home page but to the photo page, as seen above. It is a dynamic URL, so I need to extract the dog-IMG1234 part out of the original file request and put on the end of my photo.cgi script.


RewriteEngine on

RewriteCond %{HTTP_REFERER} !^http://www.mysite.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|JPG)$ http://www.mysite.com [L]
9:22 pm on Feb 4, 2013 (gmt 0)

10+ Year Member



So I need to remove the URL up until dog-IMG1234, then remove the _file4.JPG part too so I am left with just the dog-IMG1234 part. Thanks!
11:43 pm on Feb 4, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



[httpd.apache.org...]

The part you want is
>> RewriteCond >> QueryString

While you are there, refresh your memory on the difference between a rewrite and a redirect. (The functions, not the commands.) Or just read any half-dozen threads in this forum; it's probably more intelligible :)

http://www.mysite.com [L]


The flag says rewrite (there's no R=301) but the form of the target says redirect (full protocol and hostname). The result will be a 302 redirect, which is probably the last thing you want.
12:41 am on Feb 5, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Which one is which here?

The 1st URL is an image file. The 2nd is a webpage with the photo on it.

I understand the question even less than I did before.
12:52 am on Feb 5, 2013 (gmt 0)

10+ Year Member



Image File: (ends with JPG)
http://www.mysite.com/images/photos/dog-IMG1234_file4.JPG

Webpage URL
http://www.mysite.com/cgi-bin/photo.cgi?dog-IMG1234
2:02 am on Feb 5, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Er, yes, we understand that. We understood it the first time and the second time too. We're past that.

g1, I think berm wants something involving

/cgi-bin/photo.cgi?$1

--assuming you can do this with image files-- but I'm waiting for some elucidation about where the hotlinking comes in. So far the proposed RewriteRule is silent about both refers and query strings.

Then again, I've yet to work out how to serve up something with a non-image extension when an image is requested in <img src> form. Possibly it can't be done on my hosting setup, so I should stop beating my brains out over it.