PHP: Một số cách sử dụng .htaccess để cấu hình website - P1

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực

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:
    1
    2
    # enable basic rewriting
    RewriteEngine 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.
    1
    2
    # enable symbolic links
    Options +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ộ):
    1
    2
    3
    4
    # 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:
    1
    2
    # increase performance by disabling allowoverride
    AllowOverride 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:
    1
    2
    # rename htaccess files
    AccessFileName 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):
    1
    2
    3
    4
    5
    # protect renamed htaccess files
    <FilesMatch "^ht\.">
    Order deny,allow
    Deny 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ự:
    1
    2
    # pass the default character set
    AddDefaultCharset utf-8
  • Cài đặt server Timezone: ngoài ra, bạn có thể tham khảo thêm tại đây.
    1
    2
    # set the server timezone
    SetEnv TH Asia/Bangkok
  • Enable file caching:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    # cache public/images/article 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 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
    <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:
    1
    2
    3
    4
    # set the default language
    DefaultLanguage en-US
    # set the default character set
    AddDefaultCharset UTF-8
  • Gửi kiểu mã hóa và header không cần thẻ meta
    1
    2
    3
    4
    # send the language tag and default character set
    # AddType 'text/html; charset=UTF-8' html
    AddDefaultCharset UTF-8
    DefaultLanguage en-US
  • Giới hạn các request GET và PUT
    1
    2
    3
    4
    # limit server request methods to GET and PUT
    Options -ExecCGI -Indexes -All
    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD) RewriteRule .* - [F]
  • Lựa chọn file xử lý theo phương thức request tới server
    1
    2
    3
    # process files according to server request method
    Script PUT /cgi-bin/upload.cgi
    Script GET /cgi-bin/download.cgi
  • Thực thi một định dạng file bằng 1 cgi script
    1
    2
    # execute all png files via png-script.cgi
    Action image/png /cgi-bin/png-script.cgi

Nguồn: http://wiki.matbao.net/mot-so-cach-su-dung-htaccess-de-cau-hinh-website.ashx 

» Tiếp: Một số cách sử dụng .htaccess để cấu hình website - P2
« Trước: .htaccess căn bản
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!