顯示具有 htaccess 標籤的文章。 顯示所有文章
顯示具有 htaccess 標籤的文章。 顯示所有文章

2010年3月16日 星期二

透過Apache的.htaccess 設置圖片防盜連功能


本站自從搬到美國的新主機後,原本在我自己舊主機設置的防圖片盜連功能就無效了,必須得重新設置防盜連的設定。


對租用網站主機空間的用戶而言,透過Apache的.htaccess來設置防盜連功能(anti hotlink),是最方便的。


新的設定方式如下:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://yblog.org/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://yblog.org$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yblog.org/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yblog.org$ [NC]
RewriteRule .*\.(jpg|gif|png|bmp|rar|zip|exe)$ /content/no_hotlink.jpeg [R,NC]


如果你想在自己的網站主機上也使用這樣的防盜連功能,生成一個.htaccess文件放到你欲限制的目錄(根目錄最好避免,在根目錄使用的.htaccess建議用作其它用途,欲限制的目錄下才放本文設定的防盜連方式)。在Windows平台上如果發現自己無法產生.htaccess文件,可以用記事本或其它的文字編輯軟體,另存新檔時,檔名選擇.htaccess即可。


RewriteCond可指定從哪些域名來連線是許可的。
RewriteRule則指定哪些附檔名類型不能被盜連(hotlink),後面可以指定連結到某個錯誤訊息頁面,或者是一張圖片。


如果RewriteRule這一行設定成RewriteRule \.(jpg|jpeg|gif|png|bmp|rar|zip|exe)$ - [F],則表示盜連的人,會看到403錯誤訊息,顯示禁止存取(403 Forbidden)。

如果網站上有 Flash 呼叫外部圖片,要再加上以下這行,不然 FireFox 會誤判。
RewriteCond %{HTTP_REFERER} !^$     [NC]

2009年6月15日 星期一

Apache 網頁認證.htaccess 設定方法

無法查看此摘要。請 按這裡查看文章。

2009年4月23日 星期四

使用 Apache htaccess 快速於網頁 Header/Footer 插入特定程式

當看到一卡車的檔案, 每個檔案都要進去裡面加個 include 或某些資料, 想到就很辛苦~

另一種做法就是用 Apache .htaccess 的功能, 就可以快速達到這點~

.htaccess 是對整個目錄以下的做動作, 所以下述參數會對此目錄的所有檔案都加上 header.php.

php_value auto_prepend_file / auto_append_file 設定

  • php_value auto_prepend_file "header.php" # 在每個檔案的最上方(前方), 都把 header.php 的內容插入
  • php_value auto_append_file "footer.php" # 在每個檔案的最下方(後方), 都把 footer.php 的內容插入

其它常用設定

  • DirectoryIndex index.html index.htm index.php # 此目錄下如果沒輸入檔名, 預設搜尋順序
  • AddType application/x-httpd-php .html .htm # 讓 .html .htm 都經過 php parser
  • php_value default_charset utf-8 # 預設編碼吐 UTF-8

2008年10月21日 星期二

減少css 與 javascript 的 loading

方法一: .htaccess 加入


ExpiresActive On
ExpiresDefault "access plus 10 years"

把這些檔案存取 10年. 除非他按下 ctrl + F5 重新整理.

方法二:
.htaccess
#Rules for Versioned Static Files
RewriteRule ^(scripts|css)/(.+)\.(.+)\.(js|css)$ $1/$2.$4 [L]

會將 css 與 js 檔案 從 js/jquery.123.js ==> js/jquery.js

也可以使用在圖檔
#Rules for Versioned Static Files
RewriteRule ^(scripts|css|images)/(.+)\.(.+)\.(js|css|jpg|gif|png)$ $1/$2.$4 [L]

搭配php function

function autoVer($url){
$path = pathinfo($url);
$ver = '.'.filemtime($_SERVER['DOCUMENT_ROOT'].$url).'.';
echo $path['dirname'].'/'.str_replace('.', $ver, $path['basename']);
}
include($_SERVER['DOCUMENT_ROOT'].'/path/to/autoVer.php');
<link rel="stylesheet" href="<?php autoVer('/css/structure.css'); ?>" type="text/css" />
<script type="text/javascript" src="<?php autoVer('/scripts/prototype.js'); ?>"></script>

結果變成:


<link rel="stylesheet" href="/css/structure.1194900443.css" type="text/css" />
<script type="text/javascript" src="/scripts/prototype.1197993206.js"></script>

出處:http://particletree.com/notebook/automatically-version-your-css-and-javascript-files/

2008年6月26日 星期四

.htaccess 實用範例

以下是一些實用的 .htaccess 範例,可以不改動 php.ini 及 httpd.conf 便設定伺服器變數:

關閉 magic_quotes_gpc

# Only if you use PHP

php_flag magic_quotes_gpc off

減少 Bandwidth 使用量

# Only if you use PHP

php_value zlib.output_compression 16386

變更 scripts 副檔名

AddType application/x-httpd-php .php4

以上語句會將 .php4 當成 PHP 程式編譯。

禁止 .htaccess 開放瀏覽


order allow,deny
deny from all

變更預設頁面

DirectoryIndex myhome.htm index.htm index.php

自訂錯誤頁面

ErrorDocument 403 /forbidden.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /servererror.html

開放/禁止瀏覽目錄

# 禁止
Options All -Indexes
# 開啟
Options +Indexes

用 .htaccess 強制顯示下載對話框

在網頁上直接連結到如 .avi 及 .pdf 檔案時,瀏覽器不會彈出下載對話框,而會直接在瀏覽器顯示內容。這個問題可以透 .htaccess 解決,強制指定檔案顯示下載對話框,例如:

AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf