一、.htaccess的基本作用 .htaccess是一個(gè)純文本文件,它里面存放著Apache服務(wù)器配置相關(guān)的指令。
<Directory /> Options FollowSymLinks AllowOverride None </Directory> 改為: <Directory /> Options FollowSymLinks AllowOverride All </Directory>
AccessFileName .config
三、.htaccess訪問控制 1、訪問控制基礎(chǔ):Order命令 為了限制用戶訪問一些關(guān)鍵目錄,通常加入.htaccess文件,常見的寫法如下: <Files ~ "^.*\.([Ll][Oo][Gg])|([eE][xX][eE])"> Order allow,deny Deny from all </Files>
說明: (1)Files后的波浪線表示啟用“正則表達(dá)式”,簡單的寫法有:<Files *>。 (2)Order命令:通過Allow,Deny參數(shù),Apache首先找到并應(yīng)用Allow命令,然后應(yīng)用Deny命令,以阻止所有訪問,也可以使用Deny,Allow。 四、URL重寫 下面是一段簡單的URL重寫規(guī)則示例: # 將 RewriteEngine 模式打開 RewriteEngine On # Rewrite 系統(tǒng)規(guī)則請(qǐng)勿修改 RewriteRule ^p/([0-9]+)\.html$ index.php?post_id=$1 RewriteRule ^u-(username|uid)-(.+)\.html$ space.php?$1=$2
五、配置錯(cuò)誤頁面 基本語法如下: # custom error documents ErrorDocument 401 /err/401.php ErrorDocument 403 /err/403.php ErrorDocument 404 /err/404.php ErrorDocument 500 /err/500.php
六、htaccess常用命令和配置技巧 Options -Indexes
deny from 000.000.000.000
allow from 000.000.000.000
deny from all
DirectoryIndex index.php index.php3 messagebrd.pl index.html index.htm
Redirect /location/from/root/file.ext http:///new/file/location.xyz
/oldfile.html
/old/oldfile.html
Redirect /olddirectory http: ///newdirectory
http: ///olddirecotry/oldfiles/images/image.gif
http: ///newdirectory/oldfiles/images/image.gif
七、安全配置 RewriteBase /
RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www.)?/.*$ [NC] RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
RewriteEngine On
# proc/self/environ? 沒門! RewriteCond %{QUERY_STRING} proc/self/environ [OR] # 阻止腳本企圖通過URL修改mosConfig值 RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|%3D) [OR] # 阻止腳本通過URL傳遞的base64_encode垃圾信息 RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR] # 阻止在URL含有<script>標(biāo)記的腳本 RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR] # 阻止企圖通過URL設(shè)置PHP的GLOBALS變量的腳本 RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR] # 阻止企圖通過URL設(shè)置PHP的_REQUEST變量的腳本 RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2}) # 把所有被阻止的請(qǐng)求轉(zhuǎn)向到403禁止提示頁面! RewriteRule ^(.*)$ index.php [F,L]
# 禁止某些目錄里的腳本執(zhí)行權(quán)限
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi Options -ExecCGI
Redirect 301 http://www./home http://www./
AddType application/octet-stream .pdf
AddType application/octet-stream .zip AddType application/octet-stream .mov
RewriteEngine On
RewriteBase / RewriteCond %{HTTP_HOST} ^www. [NC] RewriteRule ^(.*)$ http:///$1 [L,R=301]
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php ErrorDocument 404 /error/404.php ErrorDocument 500 /error/500.php
# 壓縮 text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript .AddOutputFilterByType DEFLATE application/x-javascript
<FilesMatch “.(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$”>
Header set Cache-Control “max-age=2592000″ </FilesMatch>
# 顯式的規(guī)定對(duì)腳本和其它動(dòng)態(tài)文件禁止使用緩存
<FilesMatch “.(pl|php|cgi|spl|scgi|fcgi)$”> Header unset Cache-Control </FilesMatch>
|
|