Welcome to WebmasterWorld Guest from 54.159.105.39

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite URL

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

New User

10+ Year Member

joined:Jan 17, 2005
posts: 12
votes: 0


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)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


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)

New User

10+ Year Member

joined:Jan 17, 2005
posts: 12
votes: 0


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)

New User

10+ Year Member

joined:Jan 17, 2005
posts: 12
votes: 0


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)

New User

10+ Year Member

joined:Jan 17, 2005
posts: 12
votes: 0


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)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13735
votes: 458


[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)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


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)

New User

10+ Year Member

joined:Jan 17, 2005
posts: 12
votes: 0


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)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13735
votes: 458


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.