Forum Moderators: phranque

Message Too Old, No Replies

rewriting a URL

A problem with escaping characters properly?

         

Broadway

7:17 pm on Aug 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Some one, in a sizable way, is hotlinking to one of my images. I have anti-hotlinking for images enabled via my htaccess file but the problem is that the hotlinker is linking with a non-sensical URL. If I can just rewrite the url filename then the anti-hotlinking should work.

In my log files I see:
"GET /my-directory/s%3Cimg%20src= HTTP/1.1"

(Notice there is no file extension, nothing that indicates that the call if for an image.)

I have tried the following rewriterule forms but they don't work. I would assume it has to do with characters in the filename that need to be escaped. I have tried both of the following forms but neither works. Where does my error lie?


RewriteRule ^my-directory/s\%3Cimg\%20src\=$ http://example.com/my-directory/new.gif

RewriteRule ^my-directory/s\%3Cimg\%20src=$ http://example.com/my-directory/new.gif

phranque

9:43 am on Aug 18, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



why don't you serve a 404 status code?
it is the appropriate response for those requests. (Not Found)

Broadway

1:32 pm on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a 404 page that gets triggered when this graphic is requested (per what I see in my logs). Since looking for a call of that page in my logs is one way I discover website problems, I was trying to do this rewrite as a way of cleaning up my logs and making evaluating them easier (this graphic is requested that often).

jdMorgan

4:31 am on Aug 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The %3C is a URL-encoded "<" character, and %20 is an encoded space. This malformed link is likely the result of incorrectly-implemented auto-linking code -- or someone trying to type in HTML source code as the image link. Something like "<img src=http://www.example.com/my-directory/correct-image-filename.jpg>" was the likely intent.

So, one of these should work -- try the simple one first:

RewriteRule ^my-directory/s<img\ src=$ http://example.com/my-directory/new.gif [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /my-directory/s\%3Cimg\%20src=\ HTTP/
RewriteRule ^my-directory/s http://example.com/my-directory/new.gif [R=301,L]

Also, consider whether you really want or need to redirect the client, asking it to make a second request. An internal rewrite to the "new.gif" image might be better since it would save work for your server and only one log entry will be made per hotlink request...

That would be a simple matter of changing the substitution path of the rules above from "http://example.com/my-directory/new.gif [R=301,L]" to just "my-directory/new.gif [L]"

Jim