Chúng ta sẽ đi qua 1 số cấu hình mà tôi cho là cần thiết đối với website của bạn.
- Enable basic rewriting : Server có thể không bật chế độ “mod_rewite” mặc định, để đảm bảo chế độ này được bật, thêm vào file .htaccess tại thư mục root:12
# enable basic rewritingRewriteEngine on - Enable Symbolic links : trước tiên bạn hãy tìm hiểu về Symbolic links và để chế độ này hoạt động, tính năng: AllowOverride Options cần được enable.12
# enable symbolic linksOptions +FollowSymLinks - Enable AllowOverride : Đối với các chỉ thị cần tính năng AllowOverride để thực thi như: FollowSymlinks, … Khi cần enable tính năng này tại một thư mục nào đó, ta thêm vào .htaccess (Có thể cấu hình tại file server để áp dụng toàn bộ):1234
# enable allowoverride privileges<Directory/www/replace/this/with/actual/directory>AllowOverride Options</Directory>
Việc cấu hình AllowOverride ở thư mục gốc, server sẽ phải tìm kiếm ở tất cả các thư mục để xem nơi nào .htaccess tồn tại, điều này làm chậm tốc độ xử lý. Để hạn chế điều này, disable chế độ AllowOverride tại thư mục gốc và bật lên ở những nơi cần dùng, để disable:12# increase performance by disabling allowoverrideAllowOverride None - Đặt tên lại file .htaccess :tùy biến chúng ta có thể thay đổi tên này và thực hiện trên file cấu hình của server:12
# rename htaccess filesAccessFileName ht.access
Khi thay đổi tên file .htaccess, cần cập nhật tất cả các cấu hình liên quan. Ví dụ: nếu bạn bảo vệ .htaccess với FilesMatch, định dạng lại file này (với .htaccess đã đổi thành: ht.access):12345# protect renamed htaccess files<FilesMatch"^ht\.">Order deny,allowDeny from all</FilesMatch> - Kế thừa cấu hình từ httpd.conf:1
RewriteOptions Inherit - Tăng hiệu năng bằng cách truyền tập các ký tự:12
# pass the default character setAddDefaultCharset utf-8 - Cài đặt server Timezone: ngoài ra, bạn có thể tham khảo thêm tại đây.12
# set the server timezoneSetEnv TH Asia/Bangkok - Enable file caching:12345678910111213141516171819202122232425262728293031
# cache images and flash content for one month<FilesMatch".(flv|gif|jpg|jpeg|png|ico|swf)$">Header set Cache-Control "max-age=2592000"</FilesMatch># cache text, css, and javascript files for one week<FilesMatch".(js|css|pdf|txt)$">Header set Cache-Control "max-age=604800"</FilesMatch># cache html and htm files for one day<FilesMatch".(html|htm)$">Header set Cache-Control "max-age=43200"</FilesMatch># implement minimal caching during site development<FilesMatch"\.(flv|gif|jpg|jpeg|png|ico|js|css|pdf|swf|html|ht m|txt)$">Header set Cache-Control "max-age=5"</FilesMatch># explicitly disable caching for scripts and other dynamic files<FilesMatch"\.(pl|php|cgi|spl|scgi|fcgi)$">Header unset Cache-Control</FilesMatch># alternate method for file cachingExpiresActive OnExpiresDefault A604800 # 1 weekExpiresByType image/x-icon A2419200 # 1 monthExpiresByType application/x-javascript A2419200 # 1 monthExpiresByType text/css A2419200 # 1 monthExpiresByType text/html A300 # 5 minutes# disable caching for scripts and other dynamic files<FilesMatch"\.(pl|php|cgi|spl|scgi|fcgi)$">ExpiresActive Off</FilesMatch> - Bảng quy đổi thời gian:
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 - Cài đặt ngôn ngữ và kiểu mã hóa mặc định:1234
# set the default languageDefaultLanguage en-US# set the default character setAddDefaultCharset UTF-8 - Gửi kiểu mã hóa và header không cần thẻ meta1234
# send the language tag and default character set# AddType 'text/html; charset=UTF-8' htmlAddDefaultCharset UTF-8DefaultLanguage en-US - Giới hạn các request GET và PUT1234
# limit server request methods to GET and PUTOptions -ExecCGI -Indexes -AllRewriteEngine onRewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD) RewriteRule .* - [F] - Lựa chọn file xử lý theo phương thức request tới server123
# process files according to server request methodScript PUT /cgi-bin/upload.cgiScript GET /cgi-bin/download.cgi - Thực thi một định dạng file bằng 1 cgi script12
# execute all png files via png-script.cgiAction image/png /cgi-bin/png-script.cgi
