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é.
Contents
- 1 Redirect banned IPs using Apache htaccess
- 2 Generic redirect all http to https using Apache htaccess
- 3 Generic htaccess redirect www to non-www
- 4 Redirect non-www to www
- 5 Adding custom error pages using apache htaccess
- 6 Change the timezone of server
- 7 Allow only selected IP addresses access
- 8 Deny visitors by referrer
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:
1 2 3 4 5 | # 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
1 2 3 | # all http to https RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [NC,R=301,L] |
Hoặc
1 2 3 4 5 | # 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
1 2 3 | 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 http://www.bienthuy.com sang http://bienthuy.com – Generic htaccess redirect www to non-www:
1 2 3 4 5 | # Generic htaccess redirect www to non-www RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] |
Redirect non-www to www
Chuyển hướng tất cả các truy cập tới http://bienthuy.com sang http://www.bienthuy.com (Redirect non-www to www in .htaccess):
1 2 3 4 | # Generic htaccess redirect non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://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:
1 2 3 4 5 6 7 8 | 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:
1 2 | # 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:
http://php.net/manual/en/timezones.php
Allow only selected IP addresses access
Chỉ cho phép vài IP truy cập
1 2 3 4 | 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.
1 2 3 4 | RewriteCond %{HTTP_REFERER} semalt.com [NC,OR] RewriteCond %{HTTP_REFERER} abc.com [NC,OR] RewriteCond %{HTTP_REFERER} seoanalyses.com [NC] RewriteRule .* - [F] |
Same tags:
- Chặn dải IP Việt Nam truy cập bằng htaccess hoặc nginx
- Modrewrite (.htaccess) – redirect or deny visitors based on IP
Other Posts you may be interested in:
- Responsive Tables Bootstrap Examples
- Lễ vu lan báo hiếu 2020 là ngày nào?
- Cách tạo và thêm chữ ký trong Microsoft Outlook
- Cách cài đặt Ccleaner để dọn rác máy tính – Phần 1
- Creative Cloud – Adobe CC Direct Download Links
- Google PageSpeed Insights thay đổi: sử dụng dữ liệu thực từ người dùng trình duyệt Chrome
- Những mẫu bánh kem sinh nhật ngộ nghĩnh
Last updated on March 29th, 2020 at 10:48 am