Tag Archives: apache

Prevent hotlinking with apache

To allow all domain except one:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain\.com/$ [NC]
RewriteRule \.(gif|jpg|png)$ http://example.com/hotlink.png [R,L]

This will prevent holinking from the domain offendingdomain.com and serve the image from example.com instead.

Prevent more than one domain:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain\.com/$ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain2\.com/ [NC]
RewriteRule \.(gif|jpg|png)$ http://example.com/hotlink.png [R,L]

This will prevent hotlinking from the 2 offendind domains, if you need more just duplicate the second line.

Block all:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com/.*$ [NC]
RewriteRule .*\.(gif|jpe?g|png)$ http://example.com/hotlink.png [R,NC,L]

This will block all hotlinking and and serve the image from example.com instead.

 

NOTE: Be carefull not to rewrite to a image in the same domain as yours or you could create a redirect loop.

Configuring webalizer for all your sites

Webalizer is a great tool to analyse your sites traffic.

Here’s is how I’ve configured mine:

I keep my sites under /var/www/html change this according with with server configuration.

1) Install webalizer

For Debian

apt-get update && apt-get install webalizer

For CentOS

yum install webalizer

Or you could always download and compile

2) create your vhost and folder for webalizer
Here’s an example:


        DocumentRoot /var/www/html/webalizer/public_html
        ErrorLog    /var/www/html/webalizer/public_html/logs/error_log
        CustomLog    "|/usr/sbin/cronolog /var/www/html/webalizer/public_html/logs/access_log.%Y%m%d" combined
        ServerName webalizer.example.com
        ServerAlias www.webalizer.example.com

3) create this script

#!/bin/bash

DATE=`date --date='1 day ago'  +%Y%m%d`
LOGFILE="access_log.$DATE"

for i in $(ls /var/www/html/); do

cd /var/www/html/$i/logs

STATPATH=/var/www/html/webalizer/public_html/$i

if [ ! -d $STATPATH ]; then
mkdir $STATPATH
fi

webalizer -c /etc/webalizer.conf -n $i -o $STATPATH -t $i $LOGFILE

done

4) create a cronjob

0 4 * * * /storage/scripts/webstats.sh >> /dev/null 2>&1

And you’re done.