Tuesday, October 13, 2009

How to protect media file using Apache module rewrite in FreeBSD  

The main purpose in writing this article is to share how to protect media files. That is because I discovered that someone will copy my entire article and then paste it into their own Blog. In fact, we can not prevent it to happen. So I have to use the rewrite module of apache server to stop it. If you are using your machine as a web server, please follow my below steps to enable rewrite module...

  • 1. Confirm rewrite module has been enabled in your httpd.conf file.
# cat /usr/local/etc/apache22/httpd.conf | grep rewrite

LoadModule rewrite_module libexec/apache22/mod_rewrite.so
  • 2. If you have enabled virtual host function, please modify this file httpd-vhosts.conf and then add the following contents of the blue tag:

# vi /usr/local/etc/apache22/extra/httpd-vhosts.conf

<VirtualHost *:80>
ServerAdmin bryan.yu@msa.hinet.net
DocumentRoot /usr/local/www/apache22/data/ardoros
<Location />
      RewriteEngine on
      RewriteBase /
      RewriteCond %{HTTP_REFERER} !^$
      RewriteCond %{HTTP_REFERER} !^http://www.ardoros.com(.*) [NC]--->Allow this site to access media files
      RewriteRule \.(jpg|jpeg|gif|css|swf|png|bmp|rar|zip|exe)$ - [F] --->Limiting these attached file names
</Location>
ServerName www.ardoros.com
ErrorLog /var/log/dummy-www.ardoros.com-error_log
CustomLog /var/log/dummy-www.ardoros.com-access_log common
</VirtualHost>

  • 3. If you don't enable virtual host function, please modify this file httpd.conf in /usr/local/etc/apache22/ directory

# vi /usr/local/etc/apache22/httpd.conf

<ifmodule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{HTTP_REFERER} !^$
      RewriteCond %{HTTP_REFERER} !^http://www.ardoros.com(.*) [NC]
      RewriteRule \.(jpg|jpeg|gif|css|swf|png|bmp|rar|zip|exe|mp3)$ - [F]
</ifmodule>
  • 4. When you have completed the above steps, please modify Wordpress related file .htaccess that can be found in your website root directory. Adding the following contents of the red tag

# vi /usr/local/www/apache22/data/your_web_directory/.htaccess

# BEGIN WordPress
#

<ifmodule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{HTTP_REFERER} !^$
      RewriteCond %{HTTP_REFERER} !^http://www.ardoros.com(.*) [NC]
      RewriteRule \.(jpg|jpeg|gif|css|swf|png|bmp|rar|zip|exe|mp3)$ - [F]
</ifmodule>


# END WordPress

Stumble Upon Toolbar Digg! diigo it

Related Posts by Categories