關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • 用 mod_rewrite 防止偷圖

    Posted on March 10th, 2006 Sam Tang No comments

    以前我有一位客戶,他的網站流量十分大,經過檢查後發覺大部份的流量都用在圖片上,這也無可厚非,他的圖片解像度比較高,但另一個原因是有人在其他論壇貼上他的圖片,而貼圖的方法是直接使用他的圖片連結。我問過他的意見,他不介面別人貼出他的圖片,直接下載圖片再重新上載的話沒有問題,因為每張圖片也有出現他的網站名稱,只是不想人家浪費了他的流量。

    然後我覺得用 mod_rewrite 是較方便的做法,而如果你的網站也支援 mod_rewrite 功能,在 .htaccess 加上以下內容,也可防止給別人偷圖:

    RewriteEngine On
    RewriteCond %{HTTP_REFERER} ^http://www.anotherwebsite.com/.*$ [NC]
    RewriteRule ^/img/indieband\.jpg$ http://www.mydomain.com/nkotb.jpg [R]

    以上的第一行是開放 RewriteEngine,如果你的 .htaccess 已經有這一行就不用加上;第二行是其他網站的網址;第三行是將 /img/indieband.jpg 導向 /nkotb.jpg,而這個 /nkotb.jpg 可以是說明圖片出處的圖片。


    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word