Đơn Vị Thiết Kế Website Chuẩn SEO - Uy Tín Chuyên Nghiệp
Cộng Đồng Theme - Cách tạo script chạy .htaccess bằng SSH tăng bảo mật website

Cách tạo script chạy .htaccess bằng SSH tăng bảo mật website

Cách Tạo Script Chạy .htaccess Bằng Ssh Tăng Bảo Mật Website Cộng Đồng Web
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,

    1. 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
    2. 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

    3. 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

      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é!

Tin Tức Liên Quan

Cách Tăng Bảo Mật Website WordPress Bằng .htaccess

Cách Tăng Bảo Mật Website WordPress Bằng .htaccess

Vô hiệu hóa thực thi PHP trong thư mục /wp-content/uploads Để làm điều này bạn chỉ cần thêm rules sau vào trong file .htaccess của bạn   1 RewriteRule ^wp-content/uploads/.*.(?:php[1-7]?|pht|phtml?|phps|js)$ – [NC,F] Bảo vệ tệp tin wp-config.php Để bảo vệ tệp wp-config.php của bạn khỏi sự truy cập trái phép, chỉ cần rule sau vào...

Cách bỏ hạn chế modsecurity Server

Cách bỏ hạn chế modsecurity Server

Cách bỏ hạn chế modsecurity Server Lỗi phát sinh khi bị hạn chế modsecurity là khi update uxbuilder không load được không thêm item menu lưu lại bị trả 406 Cách khắc phục cd /usr/local/directadmin/custombuild ./build set modsecurity no ./build set modsecurity_ruleset comodo ./build modsecurity ./build modsecurity_rules ./build rewrite_confs Vậy là xong, các bạn test...

Hướng Dẫn Gỡ IP Đang Bị Block Trên Server SSH

Hướng Dẫn Gỡ IP Đang Bị Block Trên Server SSH

Hướng Dẫn Gỡ IP Đang Bị Block Trên Server SSH Chào các bạn, trong quá trình sử dụng các bạn sẽ không tránh được việc ip mình sử dụng bị hệ thống block vì nhiều nguyên nhân, trong đó nhiều nhất là nhập sai mật khẩu nhiều lần hoặc truy xuất nhiều nguồn lạ dẫn...

Hướng dẫn Đổi Port DirectAdmin (change Port DirectAdmin)

Hướng dẫn Đổi Port DirectAdmin (change Port DirectAdmin)

Hướng dẫn Đổi Port DirectAdmin (change Port DirectAdmin) Trong bài hướng dẫn hôm nay mình sẽ chỉ các bạn cách đổi Port DirectAdmin để giúp gia tăng khả năng bảo mật của DirectAdmin 1. Tìm Hiểu Port Dreactadmin Mặc định khi cài đặt và  sử dụng DirectAdmin chúng ta sẽ sử dụng Port 2222 mặc định để đăng...

Quét Virus/Malware/Shell trên VPS với Linux Malware Detect và ClamAV

Quét Virus/Malware/Shell trên VPS với Linux Malware Detect và ClamAV

Quét Virus/Malware/Shell trên VPS với Linux Malware Detect và ClamAV Linux Malware Detect (MalDet hoặc LMD) cùng với ClamAV (Antivirus Engine) là công cụ rất hữu hiệu để quét các loại malware (virus, spyware và adware) khỏi VPS/Server.Nên mình sẽ hướng dẫn các bạn cách cài đặt trên CentOS 7/6 để giải quyết vấn đề server bị virus nhé....

Bình Luận Của Bạn

24/7

hỗ trợ zalo cong dong web
Liên hệ
Hỗ trợ tại Cộng Đồng Web