Đôi lúc bạn cần bảo vệ 1 URL trên website của mình bằng 1 form đăng nhập đơn giản, hãy thêm đoạn code sau vào file .htaccess để Apache bắt buộc người dùng phải nhập username và password.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #set an environtment variable "auth" if the request starts with "/user" SetEnvIf Request_URI ^/<path-to-web-url> auth=1 AuthName "Input ID and Password." AuthType Basic AuthUserFile /<path-to-htpasswd-file> # First, allow everybody Order Allow,Deny Satisfy any Allow from all Require valid-user # Then, deny only if required Deny from env=auth |
Trong đó <path-to-web-url> là đường dẫn URL trên website của bạn và <path-to-htpasswd-file> là đường dẫn đến file .htpasswd (lưu username và password) trên server của bạn.