Apache htaccess: Biên Thùy xin giới thiệu với các bạn một số thủ thuật với htaccess Mod Rewrite như chặn IP theo list, chuyển hướng truy cập dựa vào IP, tạo trang báo lỗi theo ý mình, chuyển hướng từ www sang không có www hay ngược lại …
Đây là các code được tổng hợp dựa trên thực tế sử dụng của Biên Thùy, các bạn yên tâm sử dụng nhé.

Redirect banned IPs using Apache htaccess

Chuyển hướng tất cả các IP trong list sang một địa chỉ website khác – Redirect banned IPs:

# Redirect banned IPs
RewriteCond %{REMOTE_ADDR} 123.24.* [OR]
RewriteCond %{REMOTE_ADDR} 123.25.26.* [OR]
RewriteCond %{REMOTE_ADDR} 123.24.25.26
RewriteRule ^.*$ https://www.google.com [R=307,L]

Như ví dụ bên trên là chuyển hướng tất cả các IPs thuộc dải 123.24.x.x hoặc 123.25.26.x hoặc đích danh IP: 123.24.25.26 (x <=255) sang trang https://www.google.com. Nếu muốn block IP range thì bạn có thể tham khảo thêm bài viết Block Vietnam IP Ranges

Generic redirect all http to https using Apache htaccess

Chuyển hướng từ http sang https – Generic htaccess redirect all http to https

# all http to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [NC,R=301,L]

Hoặc

# all http to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Hoặc

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://bienthuy.com%{REQUEST_URI} [R=301,L,NE]

Generic htaccess redirect www to non-www

Chuyển hướng tất cả các truy cập tới https://www.bienthuy.com sang https://bienthuy.com – Generic htaccess redirect www to non-www:

# Generic htaccess redirect www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Redirect non-www to www

Chuyển hướng tất cả các truy cập tới https://bienthuy.com sang https://www.bienthuy.com (Redirect non-www to www in .htaccess):

# Generic htaccess redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Adding custom error pages using apache htaccess

Tạo trang báo lỗi theo ý mình – Adding custom error pages:

ErrorDocument 401 /bienthuy.shtml
ErrorDocument 402 /bienthuy.shtml
ErrorDocument 403 /bienthuy.shtml
ErrorDocument 404 /bienthuy.shtml
ErrorDocument 500 /bienthuy.shtml
ErrorDocument 501 /bienthuy.shtml
ErrorDocument 502 /bienthuy.shtml
ErrorDocument 503 /bienthuy.shtml

Change the timezone of server

Đổi múi giờ mặc định trên server – change the timezone of server to the local time:

# Changing the server timezone to the local timezone
php_value date.timezone Asia/Ho_Chi_Minh

Danh sách các timezone phù hợp có thể tìm tại đây:
https://php.net/manual/en/timezones.php

Allow only selected IP addresses access

Chỉ cho phép vài IP truy cập

order deny,allow
deny from all
allow from 12.34.56.78
allow from 10.20.30.40

Deny visitors by referrer

Chặn hết các truy cập có xuất phát từ một số trang nhất định. Ví dụ trên trang abc.com có link đến trang của mình và mình muốn chặn truy cập đó, khi khách nhấn vào link trên trang abc.com sang trang của mình thì sẽ báo lỗi 403.

RewriteCond %{HTTP_REFERER} semalt.com [NC,OR]
RewriteCond %{HTTP_REFERER} abc.com [NC,OR]
RewriteCond %{HTTP_REFERER} seoanalyses.com [NC]
RewriteRule .* - [F]
4.6/5 - (5563 bình chọn)

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *