congdongshop | Bảo Mật, Server | 28 Lượt xem |
.htaccess
là một file cấu hình đặc biệt của Apache/LiteSpeed dùng để điều khiển hành vi của máy chủ web ngay tại thư mục chứa nó, thường mình dùng để tạo sửa file này để tăng bảo mật cho website và chặn các truy cập lạ hoặc chặn file theo mong muốn,
Mình làm hướng dẫn cách tạo script chạy .htaccess bằng SSH này để có thể tạo tự động các website đang sử dụng, không phải làm thủ công từng site, lưu ý, nên backup 1 bản để phòng rủi rõ lỗi 403
Dưới đây là 1 đoạn code .htaccess mặc định.
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
để tạo 1 file .htacess chạy bằng script, mình sẽ tạo 1 file mới, thường mình để trong root để tiện thao tác,
-
-
Truy Cập SSH tạo 1 file .htaccess
Thường mình tạo 1 file bằng cách dùng lệnh này
nano /root/fix-htaccess.sh
-
Tạo script chạy .htacess
Mình chạy 1 đoạn thường hay dùng của mình để tăng bảo mật cho site.
#!/bin/bash HTACCESS_BLOCK=$(cat <<'EOF' # === SECURE & OPTIMIZED .HTACCESS === AddDefaultCharset UTF-8 <IfModule mod_mime.c> AddCharset UTF-8 .atom .css .js .json .rss .vtt .xml </IfModule> <IfModule mod_headers.c> Header unset ETag </IfModule> FileETag None <IfModule mod_alias.c> <FilesMatch "\.(html|htm|rtf|rtx|txt|xsd|xsl|xml)$"> <IfModule mod_headers.c> Header unset Pragma Header append Cache-Control "public" Header unset Last-Modified </IfModule> </FilesMatch> <FilesMatch "\.(css|js|json|ico|jpg|jpeg|png|gif|webp|svg|pdf|zip|ttf|woff|woff2|mp4|mp3)$"> <IfModule mod_headers.c> Header unset Pragma Header append Cache-Control "public" </IfModule> </FilesMatch> </IfModule> <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 1 month" ExpiresByType text/html "access plus 0 seconds" ExpiresByType application/json "access plus 0 seconds" ExpiresByType application/xml "access plus 0 seconds" ExpiresByType application/rss+xml "access plus 1 hour" ExpiresByType image/jpeg "access plus 4 months" ExpiresByType image/png "access plus 4 months" ExpiresByType text/css "access plus 1 year" ExpiresByType application/javascript "access plus 1 year" </IfModule> <IfModule mod_deflate.c> SetOutputFilter DEFLATE <IfModule mod_setenvif.c> <IfModule mod_headers.c> SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|pdf|mp4|mp3|zip)$ no-gzip dont-vary </IfModule> </IfModule> <IfModule mod_filter.c> AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json application/xml </IfModule> <IfModule mod_headers.c> Header append Vary: Accept-Encoding </IfModule> </IfModule> <IfModule mod_headers.c> Header set X-Content-Type-Options "nosniff" Header set X-XSS-Protection "1; mode=block" Header always edit Set-Cookie "(.*)" "$1; HttpOnly; Secure" Header always append X-Frame-Options SAMEORIGIN Header set Strict-Transport-Security "max-age=31536000" env=HTTPS Header unset X-Powered-By Header unset X-Pingback Header unset Server Header set Connection keep-alive </IfModule> <Files "xmlrpc.php"> Require all denied </Files> <Files "wp-config.php"> Require all denied </Files> <Files "license.txt"> Require all denied </Files> <Files "readme.html"> Require all denied </Files> <FilesMatch "^\.ht"> Require all denied </FilesMatch> <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] RewriteRule ^wp-admin/includes/ - [F,L] RewriteRule !^wp-includes/ - [S=3] RewriteRule ^wp-includes/[^/]+\.php$ - [F,L] RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L] RewriteRule ^wp-includes/theme-compat/ - [F,L] RewriteRule ^wp-content/uploads/.*\.(php[1-7]?|pht|phtml?|phps|js)$ - [NC,F] RewriteRule ^wp-content/plugins/.*\.(php[1-7]?|pht|phtml?|phps)$ - [NC,F] RewriteRule ^wp-content/themes/.*\.(php[1-7]?|pht|phtml?|phps)$ - [NC,F] </IfModule> # === END PATCH === EOF ) for dir in /home/*/domains/*/public_html; do if [ -d "$dir" ]; then htafile="$dir/.htaccess" # Tạo file nếu chưa có [ ! -f "$htafile" ] && touch "$htafile" if ! grep -q "SECURE & OPTIMIZED" "$htafile"; then echo "$HTACCESS_BLOCK" >> "$htafile" echo "✅ Đã thêm vào: $htafile" else echo "⏭ Đã tồn tại: $htafile" fi fi done
sau khi add mã vào file lưu lại bằng ctrl+o lưu lại và ctrl+x để thoát
-
Khởi chạy script để .htaccess hoạt động
Sau khi lưu xong thì bạn chạy lệnh đưới dể khỏi chạy nhé
-
- Dán đoạn trên vào file, ví dụ:
fix-htaccess.sh
- Cấp quyền chạy:
chmod +x fix-htaccess.sh
- Chạy:
./fix-htaccess.sh
- Dán đoạn trên vào file, ví dụ:
Sau khi chạy xong, bạn kiểm tra file .htaccess lại nhé, và lưu ý cuối cùng, khởi động lại webserver nhé, mình dùng OpenLiteSpeed
systemctl restart lsws
-
-
Vậy Là xong, bạn có thể kiểm tra và sử dụng nhé!