Forum Moderators: phranque

Message Too Old, No Replies

Characters appending to my rewrite

Have a rewrite that appears to be working but inherits 4 extra characters

         

jons

7:19 am on Jun 25, 2007 (gmt 0)

10+ Year Member



I'm trying to thwart a hotlinking of sorts...
In a vbulletin installation someone has posted their hotlink to me link as:

[mydomain.com...]
and per my logs, coming from:
[domain.com...]

I have added this type of rewrite to my .htaccess:

RewriteCond %{HTTP_REFERER} ^https?://(www\.)?domain\.com/showthread\.php\?t=12345 [NC]
RewriteRule .* /somewhere/smallfile.jpg [R=301,L]

It redirects but it isn't working how I should expect.
When a browser hits the source URL, the browser is rewritten as:

[myredirect.com...]

How does one get that

?f=1
off the end of rewritten URL?

jdMorgan

2:49 pm on Jun 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like the "f=1" is a query string attached to the requested URL. To clear that existing query string in a rewriterule, add a "?" to the end of your substitution URL (it won't show in the real URL):

RewriteRule .* /somewhere/smallfile.jp[b]g?[/b] [R=301,L]

To prevent redirection looping, you should exclude the substitute file from being redirected:

RewriteCond %{REQUEST_URI} !^/somewhere/smallfile\.jpg$
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?domain\.com/showthread\.php\?t=12345 [NC]
RewriteRule .* /somewhere/smallfile.jpg? [R=301,L]

or more compactly:

RewriteCond %{HTTP_REFERER} ^https?://(www\.)?domain\.com/showthread\.php\?t=12345 [NC]
RewriteRule !^somewhere/smallfile\.jpg$ /somewhere/smallfile.jpg? [R=301,L]

Also, be aware that this can cause problems if the requested resource is NOT an image file, so you might want to check that:

RewriteCond %{REQUEST_URI} !^/somewhere/smallfile\.jpg$
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?domain\.com/showthread\.php\?t=12345 [NC]
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ /somewhere/smallfile.jpg? [R=301,L]

Replace the broken pipe "¦" characters in the rule above with solid pipes before use; Posting on this forum modifies the pipe characters.

Also be aware that some portion of the requests from that site will come to your server without an HTTP_REFERER header, so they won't be redirected. Because it is up to the client to send that header, and because intervening corporate and ISP caching proxies may effectively remove that headers, there's nothing we can do about it when using referrer-based access control.

Flush your browser cache before testing any new code on your server.

Jim

jons

6:43 pm on Jun 25, 2007 (gmt 0)

10+ Year Member



The compact one kept looping even though I used your tips. But This works great for me:
RewriteCond %{REQUEST_URI}!^/somewhere/smallfile\.jpg$
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?domain\.com/showthread\.php\?t=12345 [NC]
RewriteRule .* /somewhere/smallfile.jpg? [R=301,L]

Thanks!