เราสามารถป้องกันไม่ให้โดนขโมยใช้แบนด์วิทโดยใช้ Rewrite โดยเพิ่มข้อความต่อไปนี้ที่ไฟล์ .htaccess
ตัวอย่างนี้ป้องกันไม่ให้เว็บ test.com และ kapeek.com มา hotlink รูปภาพจากเรา
RewriteEngine On RewriteCond %{HTTP_REFERER} ^http://(.+.)?test.com/ [NC,OR] RewriteCond %{HTTP_REFERER} ^http://(.+.)?kapeek.com/ [NC] RewriteRule .*.(jpe?g|gif|bmp|png)$ – [F] ที่มา: http://altlab.com/htaccess_tutorial.html http://underscorebleach.net/jotsheet/2004/11/stop-image-hotlinking-tutorial-htaccess-apache
mod_rewrite นี่ทำอะไรได้มากมายครับ วันนี้มีเหตุจำเป็นที่ผมจะต้องทำการ redirect IP Address สามารถทำได้ 2 อย่าง
1. ทำการ Redirect IP Address ของทุกๆ คน ยกเว้น IP ของคุณเอง
# Get mod_rewrite going
Options -MultiViews +FollowSymLinks
RewriteEngine On
# Redirect everyone who’s not from your IP
RewriteCond %{REMOTE_ADDR} !123.456.789.10 [NC]
RewriteRule !maintenance.html$ maintenance.html [R]
2. Redirect เฉพาะบาง IP Address ที่ต้องการเท่านั้น
พอดีนั่งดู log file แล้วเห็นว่ามีคนแอบเอาภาพเราไปใช้นี่นา ซึ่งจริงๆ แล้วผมไม่ได้ซีเรียสอะไรหรอกครับ แต่แค่อยากลองหาอะไรทำสนุกๆ ดูว่าเราจะหาวิธีป้องกันได้ยังไงบ้าง
ลองค้นๆ ดูแล้วก็พบว่าตัวช่วยที่ดีตัวหนึ่งก็คือ .htaccess และ Rewrite module ของ Apache นี่เอง
วิธีการง่ายๆ คือสร้างไฟล์ชื่อ .htaccess แล้วใส่คำสั่งของ Rewrite module ไว้ข้างใน
ดูตัวอย่างเลยก็แล้วกันครับ
1. วิธีนี้คือป้องกันไม่ให้ใครแอบเรียกไฟล์รูปภาพที่มีนามสกุล jpg|jpeg|gif|png|bmp ของเราไปใช้ โดยเมื่อภาพเหล่านี้ถูกเรียกใช้จากเว็บอื่น ภาพที่ปรากฎจะเป็น dot.gif แทน นอกจากนี้จะไม่สามารถเรียกไฟล์นั้นโดยตรงได้ด้วย เช่น จะเรียก http://my.diary.in.th/images/test.jpg ไม่ได้
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://diary.in.th/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://diary.in.th$ [NC]
RewriteCond [...]
ผมเคยใช้งาน mod_rewrite กับ .htaccess นานมาแล้ว จนลืมไปหมด วันนี้ก็บังเอิญไปเจอเว็บที่น่าสนในเกี่ยวกับเรื่องนี้ครับ
Widexl.com – Module Rewrite URL Rewriting Guide
Corz.org more .htaccess tips and tricks..
Apache Mod_Rewrite URL Rewriting Engine documentation
URL Rewriting Guide
Tags
.htaccess Apache blogspot CentOS codeigniter CSS domain name driver Drupal Flickr Hotmail HTML internet law linux mailing list mailman MICT mod_rewrite monitor MSL MySQL name server Network open source PHP Postfix Security Smarty sony Spammail SPF theme TLS traffic log Universiade UTF-8 UTF8 vbulletin Wordpress การเมือง จตุคาม รูปภาพ สถิติ เรารักในหลวง
