File .htaccess là file cấu hình của Apache web server, cho phép bạn kiểm soát cách server xử lý request mà không cần quyền admin server. Nằm trong thư mục public_html.
Mở và chỉnh sửa .htaccess:
cPanel → File Manager → public_html → tìm .htaccess (nếu không thấy, bật hiện file ẩn: Settings → Show Hidden Files) → chuột phải → Edit
Luôn backup trước khi chỉnh sửa! Sai cú pháp sẽ gây lỗi 500.
==== REDIRECT ====
Redirect HTTP sang HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect www sang non-www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
Redirect non-www sang www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Redirect URL cụ thể (301 permanent):
Redirect 301 /trang-cu.html https://domain.com/trang-moi/
Redirect 301 /category/old https://domain.com/category/new/
Redirect toàn bộ domain sang domain mới:
RewriteEngine On
RewriteRule ^(.*)$ https://domain-moi.com/$1 [R=301,L]
==== BẢO MẬT ====
Chặn truy cập file nhạy cảm:
# Chặn truy cập wp-config.php
<Files wp-config.php>
Order allow,deny
Deny from all
</Files>
# Chặn truy cập .htaccess
<Files .htaccess>
Order allow,deny
Deny from all
</Files>
# Chặn xem file .log
<Files *.log>
Order allow,deny
Deny from all
</Files>
Tắt directory listing (không cho xem danh sách file):
Options -Indexes
Chặn IP cụ thể:
Order allow,deny
Allow from all
Deny from 1.2.3.4
Deny from 5.6.7.0/24
Chỉ cho phép IP cụ thể (ví dụ trang admin):
<Files wp-login.php>
Order deny,allow
Deny from all
Allow from 1.2.3.4
</Files>
Thêm Security Headers:
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
==== TỐI ƯU HIỆU NĂNG ====
Bật Browser Caching:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
Bật Gzip Compression:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/css application/javascript
AddOutputFilterByType DEFLATE application/json
</IfModule>
==== PHP SETTINGS ====
# Tăng memory limit
php_value memory_limit 256M
# Tăng upload size
php_value upload_max_filesize 64M
php_value post_max_size 64M
# Tăng thời gian thực thi
php_value max_execution_time 300
File .htaccess mặc định của WordPress:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Nếu xóa nhầm phần này, vào Settings → Permalinks → Save để tạo lại.
Test .htaccess trực tuyến:
https://htaccess.madewithlove.com — nhập URL và .htaccess để kiểm tra redirect có đúng không trước khi áp dụng.