Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
mod_rewrite and image linking [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
deoren
Apprentice
Apprentice


Joined: 23 Dec 2004
Posts: 227
Location: USA

PostPosted: Wed Feb 08, 2006 5:23 pm    Post subject: mod_rewrite and image linking [SOLVED] Reply with quote

First of all thanks for reading this post!

I searched the forums and did not find what I was looking for.

I'm wanting to replace all images that others link to from my site with an alternate image. I have found the same posting on various corners of the web but I can't seem to get them to work.

Here is a snippet from the Apache Cookbook errata page:

Code:
SetEnvIfNoCase Referer "^http://([^/]*\.)?myserver.com/" local_referrer=1
RewriteRule %{ENV:local_referrer} !=1 /Stolen-100x100.png [L]


mod_rewrite is loaded and
Code:
RewriteEngine on
is in the config file earlier on.

I get this error when trying to restart apache2:

Code:
/etc/init.d/apache2 restart

Quote:
* Apache2 has detected a syntax error in your configuration files:
Syntax error on line 79 of /usr/lib/apache2/conf/vhosts.d/myserver.com/1_www.conf:
RewriteRule: bad flag delimiters


I'm too ignorant of mod_rewrite rules to know what's the fix for this. Tips/suggestions are most welcome!

BTW, this works to deny image linking with a 403 error but isn't quite what I'm looking for.

Code:
        <FilesMatch "\.(jpg|jpeg|gif|png)$">
            SetEnvIfNoCase Referer "^http://([^/]*\.)?myserver.com/" local_referrer=1
            Order Allow,Deny
            Allow from env=local_referrer
        </FilesMatch>


Last edited by deoren on Tue Feb 14, 2006 1:39 am; edited 1 time in total
Back to top
View user's profile Send private message
goma
n00b
n00b


Joined: 04 Feb 2006
Posts: 56

PostPosted: Wed Feb 08, 2006 10:53 pm    Post subject: Reply with quote

At first glance, I wonder if the space before the "!=1" is messing things up. Remove the space and see if that fixes the problem.
Back to top
View user's profile Send private message
deoren
Apprentice
Apprentice


Joined: 23 Dec 2004
Posts: 227
Location: USA

PostPosted: Thu Feb 09, 2006 11:42 am    Post subject: Reply with quote

Thank you for your suggestion, but unfortunately the rule does nothing with that space removed.

Just to be clear here is what I tried:

Code:
        <FilesMatch "\.(jpg|jpeg|gif|png)$">
            SetEnvIfNoCase Referer "^http://([^/]*\.)?mydomain.com/" local_referrer=1
            RewriteRule %{ENV:local_referrer}!=1 /images/linking_denied.png [L]
        </FilesMatch>


If I place the space back I get the error mentioned previously. Other ideas?
Back to top
View user's profile Send private message
goma
n00b
n00b


Joined: 04 Feb 2006
Posts: 56

PostPosted: Thu Feb 09, 2006 1:02 pm    Post subject: Reply with quote

Give this a try:
Code:
<FilesMatch "\.(jpg|jpeg|gif|png)$">
    SetEnvIfNoCase Referer "^http://([^/]*\.)?mydomain.com/" local_referrer=1
    RewriteCond %{local_referrer} !^1$
    RewriteRule ^/.*$ /images/linking_denied.png [L]
</FilesMatch>
Back to top
View user's profile Send private message
deoren
Apprentice
Apprentice


Joined: 23 Dec 2004
Posts: 227
Location: USA

PostPosted: Thu Feb 09, 2006 1:43 pm    Post subject: Reply with quote

Thanks again, but no such luck.

Here is a little more info that may help:

Example of the url layout: http://www.mydomain.com/_images/animals/cats/cat.gif

I have an alias like so:

Code:
Alias /images /var/www/mydomain.com/shared/images/


Using this:
Code:
        <FilesMatch "\.(jpg|jpeg|gif|png)$">
            SetEnvIfNoCase Referer "^http://([^/]*\.)?mydomain.com/" local_referrer=1
            RewriteCond %{local_referrer} !^1$
            RewriteRule ^/.*$ /images/linking_denied.png [L]
        </FilesMatch>


does not block the image from being used. I also cleared my cache to make sure that wasn't the issue.
Back to top
View user's profile Send private message
deoren
Apprentice
Apprentice


Joined: 23 Dec 2004
Posts: 227
Location: USA

PostPosted: Sun Feb 12, 2006 8:58 pm    Post subject: Reply with quote

I'm not sure that I've solved my problem, but so far this seems to do the trick:

Code:
    # Stop image/bandwidth theft.
    RewriteCond %{HTTP_REFERER} !^http://([^/]*\.)?mydomain.com/.*$ [NC]
    RewriteCond %{REQUEST_URI} !^/images/linking_denied.png$ [NC]
    RewriteRule .(gif|jpg|jpeg|png)$ http://www.mydomain.com/images/linking_denied.png [L]


I've cobbled together different pieces of examples from the Apache docs and other info I found scattered about the web.

In order to use the FilesMatch directive I would have to use these directives within that container:

Code:
        RewriteEngine on
        Options FollowSymLinks


These directives would combine like so:

Code:
    <FilesMatch "\.(gif|jpe?g|png)$">
        RewriteEngine on
        Options FollowSymLinks
        # Stop image/bandwidth theft.
        RewriteCond %{HTTP_REFERER} !^http://([^/]*\.)?mydomain.com/.*$ [NC]
        RewriteCond %{REQUEST_URI} !^/images/linking_denied.png$ [NC]
        RewriteRule .(gif|jpg|jpeg|png)$  http://www.mydomain.com/images/linking_denied.png [L]
    </FilesMatch>


It works to also write it this way:

Code:
    <FilesMatch "\.(gif|jpe?g|png)$">
        RewriteEngine on
        Options FollowSymLinks
        # Stop image/bandwidth theft.
        RewriteCond %{HTTP_REFERER} !^http://([^/]*\.)?mydomain.com/.*$ [NC]
        RewriteCond %{REQUEST_URI} !^/images/linking_denied.png$ [NC]
        RewriteRule ^/.*$ http://www.mydomain.com/images/linking_denied.png [L]
    </FilesMatch>


and lastly if you have several virtual hosts that make use of an /images alias:


Code:
    <FilesMatch "\.(gif|jpe?g|png)$">
        RewriteEngine on
        Options FollowSymLinks
        # Stop image/bandwidth theft.
        RewriteCond %{HTTP_REFERER} !^http://([^/]*\.)?mydomain.com/.*$ [NC]
        RewriteCond %{REQUEST_URI} !^/images/linking_denied.png$ [NC]
        RewriteRule ^/.*$ http://%{SERVER_NAME}/images/linking_denied.png [L]
    </FilesMatch>


The only thing I haven't figured out how to do is have linking_denied.png not processed again once the redirect has been sent to the site visitor.

These two directives were invaluable in determining where the problem was:

Code:
    RewriteLog /var/www/mydomain.com/logs/www.rewrite.log
    RewriteLogLevel 4
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum