Cấu hình tự động chuyển HTTP → HTTPS & HSTS
Trong năm 2025, HTTPS đã trở thành tiêu chuẩn bắt buộc cho mọi website. Các trình duyệt hiện đại (Chrome, Firefox, Edge) đều đánh dấu website HTTP là “Not Secure”, đồng thời nhiều tiêu chuẩn bảo mật (PCI DSS, ISO 27001, GDPR) yêu cầu dùng HTTPS.
Vì vậy, khi triển khai SSL/TLS, quản trị viên cần cấu hình:
- Chuyển hướng toàn bộ HTTP sang HTTPS.
- Bật HSTS (HTTP Strict Transport Security) để buộc client luôn dùng HTTPS trong các lần truy cập tiếp theo.
Bài 1 — Apache
Cấu hình chuyển hướng trong VirtualHost port 80:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
- Redirect permanent → tạo 301 redirect (vĩnh viễn), giúp SEO và client cache.
- Nếu có nhiều domain alias (www.example.com, example.com), nên khai báo thêm.
Bài 2 — nginx
Cấu hình chuyển hướng toàn bộ HTTP sang HTTPS:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
- 301 thay vì 302 để đảm bảo redirect là vĩnh viễn.
- $host$request_uri giữ nguyên domain + path gốc khi redirect.
Best Practices 2025
- Luôn dùng 301 Permanent Redirect thay vì 302 để tối ưu SEO & caching.
- Bật HSTS (Strict-Transport-Security) trên HTTPS VirtualHost:
Apache
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
Nginx
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
- max-age=31536000: buộc trình duyệt nhớ HTTPS trong 1 năm.
- includeSubDomains: áp dụng cho tất cả subdomain.
- preload: có thể submit domain vào danh sách preload của Chrome/Firefox.
- Test redirect & HSTS bằng:
curl -I http://example.com
curl -I https://example.com
- Không redirect vòng lặp: kiểm tra kỹ khi có nhiều server_name hoặc reverse proxy.
Kết luận
- Apache: dùng Redirect permanent.
- nginx: dùng return 301.
- HSTS: thêm header Strict-Transport-Security để tăng cường bảo mật.
Với cấu hình trên, website sẽ luôn được truy cập qua HTTPS an toàn, đáp ứng cả yêu cầu kỹ thuật và chuẩn bảo mật quốc tế.


