File .htaccess được sử dụng rất rộng rãi, từ bảo mật đến SEO. Hôm nay mình chia sẻ cách bảo mật website bằng file .htaccess cơ bản nhất.
Giới hạn truy cập, chặn IP:
order allow,deny
deny from 000.000.000.000
allow from all
Thay 000.000.000.000 bằng IP bạn muốn chặn
Bảo mật cho một file bất kỳ bằng .htaccess:
<files config.php>
order allow,deny
deny from all
</files>
Thay config.php bằng file bạn muốn bảo mật
Tắt liệt kê nội dung thư mục:
Options All -Indexes
Tạo trang báo lỗi tùy chỉnh:
ErrorDocument 403 /error_403.html
ErrorDocument 404 /error_404.html
ErrorDocument 500 /error_500.html
ErrorDocument 503 /error_503.html
Đặt mật khẩu cho thư mục:
Đầu tiên bạn tạo file .htpasswd tại [ĐĂNG NHẬP ĐỂ XEM LIÊN KẾT]
Sau đó trong file htaccess ở thư mục cần bảo vệ bạn thêm đoạn sau:
# thông báo khi đăng nhập
AuthName "Password Protected Area"
AuthType Basic
# đường dẫn đến file .htpasswd
AuthUserFile /test/.htpasswd
require valid-user
Chống chạy file bất kỳ của bạn qua website khác:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?tenmiencuaban.com/.*$ [NC]
#RewriteRule \.(gif|jpg)$ – [F]
#RewriteRule \.(gif|jpg)$ http://www.tenmiencuaban.com/file.jpg [R,L]
Tránh trùng lặp nội dung:
# set the canonical url
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]