Hướng dẩn Một số cấu hình trên file .htaccess

htaccess là một file cấu hình sử dụng cho các web server chạy Apache. .htaccess dùng để thiết lập các tùy chọn: thực thi hay loại bỏ các chức năng, tính năng của Apache

1. .htaccess l g?

1.1. .htaccess l g

.htaccess l một file cấu hnh sử dụng cho cc web server chạy Apache. .htaccess dng để thiết lập cc ty chọn: thực thi hay loại bỏ cc chức năng, tnh năng của Apache

1.2. nghĩa k hiệu định nghĩa trong .htaccess

# : cho php server bỏ qua dng ny
[F] : Forbidden, hướng dẫn server trả về lỗi 403 cho client
[L] : Last rule, hướng dẫn server ngừng ghi lại sau khi một chỉ thị đ được xử l
[N] : Next, chỉ dẫn cho Apache trả về luật rewrite cho tới khi tất cả cc chỉ thị rewrite được hon tất.
[G]: Gone, chỉ dẫn server chuyển thng điệp Gone
[P]: Proxy, chỉ dẫn server sử dụng cc request cung cấp bởi mod_proxy
[C]: Chain, chỉ dẫn server gắn luật trước với luật sau n
[R]: Redirect, hướng dẫn Apache đưa ra một chuyển hướng
[NC]: No case, xc định bất cứ trường hợp no lin quan tới n l v l (Khng thể xảy ra)
[PT]: Pas Through: chỉ dẫn mod_rewrite để vượt qua cơ chế ghi lại URL cho cc xử l xa hơn
[OR]: Or, l c php logic bnh thường (biểu thức kết hợp đng khi một trong hai biểu thức con của n đng)
[NE]: No Escape: hướng dẫn server lọc đầu ra
[NS]: No Subresquest: hướng dẫn server giữ chỉ thị nếu l một request con từ bn trong
[QSA]: gắn thm chuỗi truy vấn vo cuối URL
[S=x]: Skip: chỉ dẫn server dừng lại x luật
[E=[/COLOR]variable:value]: Environment Variale: chỉ dẫn server thiết lập gi trị ti nguyn
[T=MIME-type]: Mime Type: m tả loại mime của ti nguyn đch
[]: xc định một tập cc k tự trong đ bất cứ k tự no c trong ngoặc xuất hiện sẽ được match
[]+: tập cc k tự trong đ bất cứ kết hợp no với n cũng được match
[a-z] : match với tất cả cc k tự từ a-z, theo bảng chữ ci. C thể c thm: [a-zA-Z]
a{n}: xc định số k tự sẽ mở rộng cng với k tự đầu, tức l khi match được k tự đầu sẽ lấy thm bao nhiu k tự tiếp theo. V dụ: x{3}sẽ lấy: xs, xad,
a{n,} : như a{n} nhưng c thể lấy 3 hoặc nhiều hơn
a{n,m}: như a{n} nhưng sẽ lấy trong khoảng từ n tới m
(): nhm cc k tự lại với nhau, xem chng như 1 đơn vị đơn lẻ
^: ghi ch bắt đầu chuỗi regex
$: ghi ch kết thc chuỗi regex
? : cho php chọn lựa k tự.V dụ: monzas? sẽ match với monza hoặcmonzas
! : m tả một phủ định, sẽ match với tất cả thứ g khc với cc k tự sau !
. : thể hiện cho bất cứ k tự đơn no
+: match với một hoặc nhiều k tự
*: match với 0 hoặc nhiều k tự
|: php hoặc logic
\: thm vo trước cc k tự đặc biệt để c thể sử dụng chng như cc k tự bnh thường
.* : khng c k tự no hoặc nhiều k tự bất kỳ
^$: định nghĩa một chuỗi rỗng
^.*$: sử dụng để match mọi thứ
[^/.]: định nghĩa 1 k tự l / hoặc .
[^/.]+: định nghĩa bất cứ số lượng k tự no chứa / hoặc .
http://: l một chuỗi bnh thường
^domain.*: định nghĩa một chuỗi bắt đầu với domain
^domain\.com$: xc định sự mở rộng của chuỗi domain.com
-d: kiểm tra nếu chuỗi l một directory
-f: kiểm tra nếu một chuỗi l một file
-s: kiểm tra nếu file trong chuỗi kiểm tra c 1 gi trị khc 0

1.3. M redirect

301 Move permanently
302 Move temporarily
403 Forbidden
404 Not found
410 Gone

1.4. Cch sử dụng .htaccess

Tạo file .htaccess (ch đy l tn đầy đủ, khng phải l phần mở rộng), tiến hnh cc thiết lập cấu hnh trong file v đặt ở trong thư mục mong muốn thực hiện cc cấu hnh đ.

V dụ:
AuthName "Member's Area Name"
AuthUserFile /path/to/password/file/.htpasswd
AuthType Basic
require valid-user
ErrorDocument 401 /error_pages/401.html
AddHandler server-parsed .html

V dụ trn cấu hnh sử dụng password bảo vệ thư mục v chuyển hướng tới trang 401.html khi gặp lỗi 401.

Ch :

- Upload file .htaccess ở chế độ ASCII thay v chế độ BINARY hay cc chế độ khc do cơ chế chuyển dữ liệu ở cc chế độ l khc nhau.
- Việc cấp quyền truy cập, sử dụng v thực thi file .htaccess c thể gy ra lỗi, ci đặt quyền 755 hoặc quyền thực thi với file
- Comment lại cc thng tin cấu hnh quan trọng để dễ dng cho người tiếp quản sau ny hoặc cho chnh bản than khi phải cấu hnh lại hoặc khắc phục sự cố no đ

2. Những cấu hnh cần thiết

2.1. Enable basic rewriting

Server c thể khng bật chế độ mod_rewite mặc định, để đảm bảo chế độ ny được bật, thm vo file .htaccess tại thư mục root:
# enable basic rewriting
RewriteEngine on

2.2. Enable Symbolic links

Xem về Symbolic links tại:http://en.wikipedia.org/wiki/Symbolic_link. Để chế độ ny hoặc động, tnh năng: AllowOverride Options cần được enable.
# enable symbolic links
Options +FollowSymLinks

2.3. Enable AllowOverride

Đối với cc chỉ thị cần tnh năng AllowOverride để thực thi như: FollowSymlinks, Khi cần enable tnh năng ny tại một thư mục no đ, ta thm vo .htaccess (C thể cấu hnh tại file server để p dụng ton bộ):
# enable allowoverride privileges

AllowOverride Options


2.4. Đặt tn lại file .htaccess

Khng phải mọi hệ thống đều thch định dạng .htaccess, c thể thay đổi tn ny (thực hiện trn file cấu hnh của server):
# rename htaccess files
AccessFileName ht.access

Khi thay đổi tn file .htaccess, cần cập nhật tất cả cc cấu hnh lin quan. V dụ: nếu bạn bảo vệ .htaccess với FilesMatch, định dạng lại file ny (với .htaccess đ đổi thnh: ht.access):
# protect renamed htaccess files

Order deny,allow
Deny from all


2.5. Giữ lại cc luật đ được định nghĩa trong httpd.conf

Tiết kiệm thời gian v nỗ lực định nghĩa lại cc luật lặp lại cho nhiều host ảo với chỉ 1 file httpd.conf, để đơn giản hơn ta cấu hnh .htaccess kế thừa tập luật từ httpd.conf
RewriteOptions Inherit

3. Hiệu năng

3.1. Tăng hiệu năng thng qua AllowOverride

Việc cấu hnh AllowOverride ở thư mục gốc, server sẽ phải tm kiếm ở tất cả cc thư mục để xem nơi no .htaccess tồn tại, điều ny lm chậm tốc độ xử l. Để hạn chế điều ny, disable chế độ AllowOverride tại thư mục gốc v bật ln ở những nơi cần dng, để disable:
# increase performance by disabling allowoverride
AllowOverride None

3.2. Tăng hiệu năng bằng cch truyền tập cc k tự
# pass the default character set
AddDefaultCharset utf-8

3.3. Tăng hiệu năng bởi việc bảo vệ bandwidth
# preserve bandwidth for PHP enabled servers

php_value zlib.output_compression 16386


3.4. Disable chữ k server
# disable the server signature
ServerSignature Off

3.5. Ci đặt server timezone
# set the server timezone
SetEnv TZ America/Washington

3.6. Đặt địa chỉ email cho quản trị server

# set the server administrator email
SetEnv SERVER_ADMINdefault@domain.com

3.7. Tăng tốc độ duyệt site bằng việc enable file caching
# cache images and flash content for one month

Header set Cache-Control "max-age=2592000"

# cache text, css, and javascript files for one week

Header set Cache-Control "max-age=604800"

# cache html and htm files for one day

Header set Cache-Control "max-age=43200"

# implement minimal caching during site development

Header set Cache-Control "max-age=5"

# explicitly disable caching for scripts and other dynamic files

Header unset Cache-Control

# alternate method for file caching
ExpiresActive On
ExpiresDefault A604800 # 1 week
ExpiresByType image/x-icon A2419200 # 1 month
ExpiresByType application/x-javascript A2419200 # 1 month
ExpiresByType text/css A2419200 # 1 month
ExpiresByType text/html A300 # 5 minutes
# disable caching for scripts and other dynamic files

ExpiresActive Off


  • *Convert common time intervals into seconds:
  • 300 = 5 minutes
  • 2700 = 45 minutes
  • 3600 = 1 hour
  • 54000 = 15 hours
  • 86400 = 1 day
  • 518400 = 6 days
  • 604800 = 1 week
  • 1814400 = 3 weeks
  • 2419200 = 1 month
  • 26611200 = 11 months
  • 29030400 = 1 year = never expires

3.8. Ci đặt ngn ngữ v kiểu m ha mặc định

# set the default language
DefaultLanguage en-US
# set the default character set
AddDefaultCharset UTF-8​

3.9. M tả MIME


MIME types l tập cc phần mở rộng của file, server cần biết tham số ny để biết n đang thao tc với loại file no. Sử dụng AddType để thm một MIME, tham số tiếp theo l loại MIME v cuối cng l phần mở rộng của file. V dụ với file MP3 hoặc SWF:
AddType application/x-shockwave-flash swf
AddType application/x-shockwave-flash .swf
AddType video/x-flv .flv
AddType image/x-icon .ico

Một số loại file khng cho chạy trực tiếp trn trnh duyệt m yu cầu download về my, loại MIME cần thiết lập l:application/octec-stream

Danh sch cc MIME v loại file tương ứng:
AddType text/html .html .htm
AddType text/plain .txt
AddType text/richtext .rtx
AddType text/tab-separated-values .tsv
AddType text/x-setext .etx
AddType text/x-server-parsed-html .shtml .sht
AddType application/macbinhex-40 .hqx
AddType application/netalivelink .nel
AddType application/netalive .net
AddType application/news-message-id
AddType application/news-transmission
AddType application/octet-stream .bin .exe
AddType application/oda .oda
AddType application/pdf .pdf
AddType application/postscript .ai .eps .ps
AddType application/remote-printing
AddType application/rtf .rtf
AddType application/slate
AddType application/zip .zip
AddType application/x-mif .mif
AddType application/wita
AddType application/wordperfect5.1
AddType application/x-csh .csh
AddType application/x-dvi .dvi
AddType application/x-hdf .hdf
AddType application/x-latex .latex
AddType application/x-netcdf .nc .cdf
AddType application/x-sh .sh
AddType application/x-tcl .tcl
AddType application/x-tex .tex
AddType application/x-texinfo .texinfo .texi
AddType application/x-troff .t .tr .roff
AddType application/x-troff-man .man
AddType application/x-troff-me .me
AddType application/x-troff-ms .ms
AddType application/x-wais-source .src
AddType application/x-bcpio .bcpio
AddType application/x-cpio .cpio
AddType application/x-gtar .gtar
AddType application/x-shar .shar
AddType application/x-sv4cpio .sv4cpio
AddType application/x-sv4crc .sv4crc
AddType application/x-tar .tar
AddType application/x-ustar .ustar
AddType application/x-director .dcr
AddType application/x-director .dir
AddType application/x-director .dxr
AddType application/x-onlive .sds
AddType application/x-httpd-cgi .cgi
AddType image/gif .gif .GIF
AddType image/ief .ief
AddType image/jpeg .jpeg .jpg .jpe .JPG
AddType image/tiff .tiff .tif
AddType image/x-cmu-raster .ras
AddType image/x-portable-anymap .pnm
AddType image/x-portable-bitmap .pbm
AddType image/x-portable-graymap .pgm
AddType image/x-portable-pixmap .ppm
AddType image/x-rgb .rgb
AddType image/x-xbitmap .xbm
AddType image/x-xpixmap .xpm
AddType image/x-xwindowdump .xwd
AddType audio/basic .au .snd
AddType audio/x-aiff .aif .aiff .aifc
AddType audio/x-wav .wav
AddType audio/x-pn-realaudio .ram
AddType audio/x-midi .mid
AddType video/mpeg .mpeg .mpg .mpe
AddType video/quicktime .qt .mov
AddType video/x-msvideo .avi
AddType video/x-sgi-movie .movie
AddType message/external-body
AddType message/news
AddType message/partial
AddType message/rfc822
AddType multipart/alternative
AddType multipart/appledouble
AddType multipart/digest
AddType multipart/mixed
AddType multipart/parallel
AddType x-world/x-vrml .wrl

3.10. Gửi kiểu m ha v header khng cần thẻ meta
# send the language tag and default character set
# AddType 'text/html; charset=UTF-8' html
AddDefaultCharset UTF-8
DefaultLanguage en-US

3.11. Giới hạn cc request GET v PUT
# limit server request methods to GET and PUT
Options -ExecCGI -Indexes -All
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD) RewriteRule .* - [F]

3.12. Lựa chọn file xử l theo phương thức request tới server
# process files according to server request method
Script PUT /cgi-bin/upload.cgi
Script GET /cgi-bin/download.cgi

3.13. Thực thi một định dạng file bằng 1 cgi script

# execute all png files via png-script.cgi
Action image/png /cgi-bin/png-script.cgi​
Mời cc bạn tham giaGroup WhiteHatđể thảo luận v cập nhật tin tức an ninh mạng hng ngy.
Lưu từ WhiteHat:Kiến thức an ninh mạng để phng chống, khng lm điều xấu.Luật php lin quan
Mask
  • Mask
4. Cc cấu hnh bảo mật

4.1. Ngăn cản truy cập file .htaccess

Khi người dng cố tnh truy nhập file .htaccess sẽ trả về lỗi 403, c nhiều cch để cấu hnh, c thể cấu hnh file với CHMOD l 644 hoặc thm đoạn m sau:
# secure htaccess file

order allow,deny
deny from all


4.2. Ngăn cản truy cập tới một file đặc biệt

Để chặn truy nhập vo một file đặc biệt no đ, thm đoạn m sau vo file .htaccess, giả sử l file secretfile.jpg
# prevent viewing of a specific file

order allow,deny
deny from all


4.3. Chặn truy cập tới nhiều file

Order Allow,Deny
Deny from all


4.4. Chống browse thư mục tri php

Đảm bảo người dng khng c đủ quyền khng thể xem ton bộ trang web dưới dạng Directory listing.
# disable directory browsing
Options All Indexes

Ngược lại để cho php người dng c thể xem dưới dạng ny, sử dụng:
# enable directory browsing
Options All +Indexes

Ngăn cản server listing directory:
# prevent folder listing
IndexIgnore *

Ngăn cản truy nhập vo cc file c định dạng no đ, sử dụng IndexIgnore
# prevent display of select file types
IndexIgnore *.wmv *.mp4 *.avi *.etc

4.5. Thay đổi trang index mặc định

C thể thay v sử dụng trang index một cch mặc định, ta cấu hnh cho server nhận một file khc c chức năng tương tự như file index (ở đyl business.html)
# serve alternate default index page
DirectoryIndex business.html

Hoặc cho một loạt file đều c thể l file index, server sẽ tm kiếm v đưa ra file đầu tin tm được l file index
# serve first available alternate default index page from series
DirectoryIndex filename.html index.cgi index.pl default.htm

4.6. Ngụy trang cho định dạng script

Để tăng cường tnh bảo mật, việc ngụy trang cho ngn ngữ kịch bản bằng việc thay đổi phần mở rộng cũng l một yếu tố cần xt đến:
# serve foo files as php files
AddType application/x-httpd-php .foo

# serve foo files as cgi files
AddType application/x-httpd-cgi .foo

4.7. Giới hạn truy cập tới mạng LAN
# limit access to local area network

order deny,allow
deny from all
allow from 192.168.0.0/33


4.8. Bảo vệ thư mục bằng địa chỉ IP v/hoặc domain

Cấu hnh cho php cc truy cập ngoại trừ truy cập từ địa chỉ: x.y.z.v v từ domain.com
# allow all except those indicated here

order allow,deny
allow from all
deny from x.y.z.v
deny from .*domain.com.*


Ngược lại với cấu hnh bn trn, từ chối tất cả IP truy cập ngoại trừ x.y.z.v v domain.com
# deny all except those indicated here

order deny,allow
deny from all
allow from x.y.z.v
allow from .*domain.com.*


Ngoi ra, cũng c thể tiết kiệm băng thng bằng cch block một số định dạng file đặc biệt như: .jpg, .zip, ,mp3, từ cc server ngoi (ở đy l abc v xyz)
# block visitors referred from indicated domains

RewriteEngine on
RewriteCond %{HTTP_REFERER} abc.com [NC,OR]
RewriteCond %{HTTP_REFERER} xyz.com [NC,OR]
RewriteRule .* - [F]


4.9. Ngăn cản hoặc cho php truy cập domain theo dải địa chỉ IP

C nhiều phương php để block một dải địa chỉ IP bằng cch cấu hnh .htaccess. Cch thức đầu tin c thể sử dụng s CIDR (Classess Inter-Domain Routing) của dải IP, cch ny hiệu quả để block cc mega-spammer như RIPE, Optinet,
# block IP range by CIDR number

order allow,deny
allow from all
deny from 10.1.0.0/16
deny from 80.0.0/8


Để cho php bởi CIDR:
# allow IP range by CIDR number

order deny,allow
deny from all
allow from 10.1.0.0/16
allow from 80.0.0/8


Một biện php khc chng ta c thể block dải IP đầu vo lin quan tới số truncating cho tới khi dải mong muốn xuất hiện
# block IP range by address truncation

order allow,deny
allow from all
deny from 99.88.77.66
deny from 99.88.77.*
deny from 99.88.*.*
deny from 99.*.*.*


Cho php địa chỉ IP theo cch ny:
# allow IP range by address truncation

order deny,allow
deny from all
allow from 99.88.77.66
allow from 99.88.77.*
allow from 99.88.*.*
allow from 99.*.*.*


4.10. Chặn hoặc cho php nhiều địa chỉ trong cng 1 dng

Block:
# block two unique IP addresses
deny from 99.88.77.66 11.22.33.44
# block three ranges of IP addresses
deny from 99.88 99.88.77 11.22.33

Allow:
# allow two unique IP addresses
allow from 99.88.77.66 11.22.33.44
# allow three ranges of IP addresses
allow from 99.88 99.88.77 11.22.33

4.11. Cc luật khc sử dụng để block hay allow địa chỉ IP

C một số luật khc c thể sử dụng:
# block a partial domain via network/netmask values
deny from 99.1.0.0/255.255.0.0

# block a single domain
deny from 99.88.77.66

# block domain.com but allow sub.domain.com
order deny,allow
deny from domain.com
allow from sub.domain.com

4.12. Ngừng cc hotlinking, lun chuyển nội dung server

Mục tiu nhằm gip cc quản trị vin ngăn cản cc website bn ngoi sử dụng trực tiếp cc hnh ảnh, nội dung, link, từ website của mnh. V dạng ny lm tốn băng thng.
Sử dụng chế độ ny khi đ kch hoạtmod_rewrite.
# stop hotlinking and serve alternate content

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?domain.com/.*$ [NC]
RewriteRule .*.(gif|jpg)$http://www.domain.com/eatme.jpg[R,NC,L]


Để chuyển giao một page lỗi thay v một số hnh ảnh như eatme.jpg như bn trn, thay dng RewriteRule bằng dng:
# serve a standard 403 forbidden error page
RewriteRule .*.(gif|jpg)$ - [F,L]

Để cho php một domain ngoi c thể sử dụng hotlinking (goodsite chẳng hạn), thm dng cấu hnh:
# allow linking from the following site
RewriteCond %{HTTP_REFERER} !^http://(www.)?goodsite.com/.*$ [NC]

4.13. Chặn Evil Robots, Site Rippers, v Offline Browsers
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR]
RewriteCond %{HTTP_USER_AGENT} ^Botmailto:craftbot@yahoo.com[OR]
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR]
RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR]
RewriteCond %{HTTP_USER_AGENT} ^Download Demon [OR]
RewriteCond %{HTTP_USER_AGENT} ^eCatch [OR]
RewriteCond %{HTTP_USER_AGENT} ^EirGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ^Express WebPictures [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro [OR]
RewriteCond %{HTTP_USER_AGENT} ^EyeNetIE [OR]
RewriteCond %{HTTP_USER_AGENT} ^FlashGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetRight [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetWeb! [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go!Zilla [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go-Ahead-Got-It [OR]
RewriteCond %{HTTP_USER_AGENT} ^GrabNet [OR]
RewriteCond %{HTTP_USER_AGENT} ^Grafula [OR]
RewriteCond %{HTTP_USER_AGENT} ^HMView [OR]
RewriteCond %{HTTP_USER_AGENT} HTTrack [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Image Stripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^Image Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} Indy Library [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^InterGET [OR]
RewriteCond %{HTTP_USER_AGENT} ^Internet Ninja [OR]
RewriteCond %{HTTP_USER_AGENT} ^JetCar [OR]
RewriteCond %{HTTP_USER_AGENT} ^JOC Web Spider [OR]
RewriteCond %{HTTP_USER_AGENT} ^larbin [OR]
RewriteCond %{HTTP_USER_AGENT} ^LeechFTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mass Downloader [OR]
RewriteCond %{HTTP_USER_AGENT} ^MIDown tool [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mister PiX [OR]
RewriteCond %{HTTP_USER_AGENT} ^Navroad [OR]
RewriteCond %{HTTP_USER_AGENT} ^NearSite [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetAnts [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Net Vampire [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Octopus [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline Explorer [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline Navigator [OR]
RewriteCond %{HTTP_USER_AGENT} ^PageGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^Papa Foto [OR]
RewriteCond %{HTTP_USER_AGENT} ^pavuk [OR]
RewriteCond %{HTTP_USER_AGENT} ^pcBrowser [OR]
RewriteCond %{HTTP_USER_AGENT} ^RealDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^ReGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^SiteSnagger [OR]
RewriteCond %{HTTP_USER_AGENT} ^SmartDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperBot [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperHTTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Surfbot [OR]
RewriteCond %{HTTP_USER_AGENT} ^tAkeOut [OR]
RewriteCond %{HTTP_USER_AGENT} ^Teleport Pro [OR]
RewriteCond %{HTTP_USER_AGENT} ^VoidEYE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web Image Collector [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebAuto [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebCopier [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebFetch [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebGo IS [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebLeacher [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebReaper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebSauger [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website eXtractor [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website Quester [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebStripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebWhacker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Wget [OR]
RewriteCond %{HTTP_USER_AGENT} ^Widow [OR]
RewriteCond %{HTTP_USER_AGENT} ^WWWOFFLE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Xaldon WebSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule ^.* - [F,L]

Thay v gửi một thng bo thn thiện, c thể gửi lại mộ website:
# send em to a hellish website of your choice
RewriteRule ^.*$http://www.hellish-website.com[R,L]

Hoặc gửi một blackhold fake email:
# send em to a virtual blackhole of fake email addresses
RewriteRule ^.*$http://english-61925045732.spampoison.com[R,L]

C thể chặn theo referrer, ở đy ta chặn iaea.org:
RewriteCond %{HTTP_REFERER} ^http://www.iaea.org$
RewriteRule !^http://[^/.].yourdomain.com.* - [F,L]

Một số cch chặn khc:
# redirect any request for anything from spamsite to differentspamsite
RewriteCond %{HTTP_REFERER} ^http://.*spamsite.*$ [NC]
RewriteRule .*http://www.differentspamsite.com[R]

# redirect all requests from spamsite to an image of something at differentspamsite
RewriteCond %{HTTP_REFERER} ^http://.*spamsite.*$ [NC]
RewriteRule .*http://www.differentspamsite/something.jpg[R]

# redirect traffic from a certain address or range of addresses to another site
RewriteCond %{REMOTE_ADDR} 192.168.10.*
RewriteRule .*http://www.differentspamsite.com/index.html[R]

4.14. Pass bảo vệ

Đặt password bảo vệ l một biện php bảo vệ nội dung website v chỉ cho php người dng nội bộ c thể truy nhập nội dung.
Được hỗ trợ bởi Apache. Gip giới hạn người dng trong những khu vực khc nhau của một website.
Khi một thư mục được đặt password th ton bộ thư mục con v file trong thư mục sẽ được đặt password như vậy.
Nội dung file .htaccess để thư mục chứa n được bảo vệ bởi mật khẩu:

username:encryptedpassword
fred_smith:eek:CF9Pam/MXJg2

Tham khảo trang sau (tạo password bảo vệ thư mục):

http://www.thejackol.com/scripts/htpasswdgen.php
# password-protect single file

AuthType Basic
AuthName "Prompt"
AuthUserFile /home/path/.htpasswd
Require valid-user


# password-protect multiple files

AuthType basic
AuthName "Development"
AuthUserFile /home/path/.htpasswd
Require valid-user


# password-protect the directory in which this htaccess rule resides
AuthType basic
AuthName "This directory is protected"
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user

# password-protect directory for every IP except the one specified
# place in htaccess file of a directory to protect that entire directory
AuthType Basic
AuthName "Personal"
AuthUserFile /home/path/.htpasswd
Require valid-user
Allow from 99.88.77.66
Satisfy Any

4.15. Tự động đặt CHMOD cho cc loại file

Cch thức ny đảm bảo đặt CHMOD cho cc loại file xc định.
# ensure CHMOD settings for specified file types
# remember to never set CHMOD 777 unless you know what you are doing
# files requiring write access should use CHMOD 766 rather than 777
# keep specific file types private by setting their CHMOD to 400
chmod .htpasswd files 640
chmod .htaccess files 644
chmod php files 600

4.16. Ngụy trang tất cả cc định dạng mở rộng

Ngụy trang tất cả cc file v coi như file .php
# diguise all file extensions as php
ForceType application/x-httpd-php

Hoặc như cc định dạng khc. Ngoi ra, c thể che giấu file php dưới cc định dạng khc:

SetHandler application/x-httpd-php


4.17. Chống tấn cng từ chối dịch vụ bằng cch giới hạn kch thước file upload
# protect against DOS attacks by limiting file upload size
LimitRequestBody 10240000

4.18. Bảo vệ thư mục bằng cch disable việc thực thi cc script
# secure directory by disabling script execution
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
Options ExecCGI

4.19. Yu cầu SSL
# require SSL
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire %{HTTP_HOST} eq "domain.tld"
ErrorDocument 403https://domain.tld

# require SSL without mod_ssl
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

5. Một số cấu hnh hữu ch khc

5.1. Kiểm tra URL
# automatically corect simple speling erors

CheckSpelling On


5.2. Sắp xếp lại trang thng bo lỗi

Cấu hnh ny rất hữu ch v n đưa ra lỗi cho người truy cập website một cch thn thiện, gip cho chủ website hiện thị cc thng bo lỗi theo cch ring.
# serve custom error pages
ErrorDocument 400 /errors/400.html
ErrorDocument 401 /errors/401.html
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

5.3. Chỉ dẫn cho browser download về my thay v chạy trực tiếp

Điều ny hữu ch đối với cc file multi media
# instruct browser to download multimedia files
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .wmv
AddType application/octet-stream .mp3
C thể p dụng với cc định dạng khc (Xem thm phần: 3.9)

5.4. Chỉ thị server hiển thị m nguồn với một số file thực thi

Một số trường hợp cần hiển thị m nguồn của một file thay v thực thi chng, sử dụng:
RemoveHandler cgi-script .pl .py .cgi

5.5. Redirect người dng tới một site tạm thời khi pht triển hoặc sửa lỗi

Trong qu trnh pht triển, bảo tr hay sửa chữa website, bạn khng muốn khch hng viếng thăm, cấu hnh dưới sẽ gip chuyển hướng người dng tới một site khc trong khi quản trị vin vẫn c khả năng truy nhập đầy đủ (x.x.x.x l IP của quản trị):
# redirect all visitors to alternate site but retain full access for you
ErrorDocument 403http://www.alternate-site.com
Order deny,allow
Deny from all
Allow from x.x.x.x

5.6. Chặn truy cập tới file hay thư mục theo thời gian
# prevent access during the midnight hour
RewriteCond %{TIME_HOUR} ^12$
RewriteRule ^.*$ - [F,L]

# prevent access throughout the afternoon
RewriteCond %{TIME_HOUR} ^(12|13|14|15)$
RewriteRule ^.*$ - [F,L]
6. Thủ thuật redirect

Đối với tất cả cc loại redirect sử dụng mode_rewrite cần enable chế độ: RewriteEngine.
# initialize and enable rewrite engine
RewriteEngine on
6.1. Redirect từhttp://www.domain.comsanghttp://domain.com
# permanently redirect from www domain to non-www domain
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.domain.tld$ [NC]
RewriteRule ^(.*)$http://domain.tld/$1[R=301,L]

6.2. Redirect từ một domain cũ sang domain mới
# redirect from old domain to new domain
RewriteEngine On
RewriteRule ^(.*)$http://www.new-domain.com/$1[R=301,L]

6.3. Redirect String Variations sang một địa chỉ

Giả sử một request c chứa string: some-string, ta sẽ chuyển request ny tới trang:http://some-string.com
# redirect any variations of a specific character string to a specific address
RewriteRule ^some-stringhttp://www.some-string.com[R]

Một số phương php khc:
# map URL variations to the same directory on the same server
AliasMatch ^/director(y|ies) /www/docs/target

# map URL variations to the same directory on a different server
RedirectMatch ^/[dD]irector(y|ies)http://domain.com

6.4. Một số redirect khc

Redirect một site đầu vo với trạng thi 301:
# redirect an entire site via 301
redirect 301 /http://www.domain.com/

Redirect một file với trạng thi 301:
# redirect a specific file via 301
redirect 301 /current/currentfile.htmlhttp://www.newdomain.com/new/new

Thủ Thuật Hay

470 Blog posts

Comments