Tiếp tục phần trước. Bài viết này mình sẽ hướng dẫn các bạn một số cách sử dụng .htaccess để cấu hình website – P3
- Kiểm tra URL
- Sắp xếp lại trang thông báo lỗi: Cấu hình này rất hữu ích vì nó đưa ra lỗi cho người truy cập website một cách thân thiện, giúp cho các bạn có thể hiển thị các thông báo lỗi theo cách riêng.
123456# server custom error pagesErrorDocument 400 /errors/400.htmlErrorDocument 401 /errors/401.htmlErrorDocument 403 /errors/403.htmlErrorDocument 404 /errors/404.htmlErrorDocument 500 /errors/500.html
- Chỉ thị server hiển thị mã nguồn với một số file thực thi : Một số trường hợp cần hiển thị mã nguồn của một file thay vì thực thi chúng.
1RemoveHandler cgi-script .pl .py .cgi
- Redirect người dùng tới một site tạm thời khi phát triển hoặc sửa lỗi: Trong quá trình phát triển, bảo trì hay sửa chửa website, bạn không muốn khách hàng viếng thăm, cấu hình dưới sẽ giúp chuyển hướng người dùng tới một site khác trong khi quản trị viên vẫn có khả năng truy nhập đầy đủ (x.x.x.x là IP của quản trị):
- Chặn truy cập tới file hay thư mục theo thời gian
12345678# prevent access during the midnight hourRewriteCond %{TIME_HOUR} ^12$RewriteRule ^.*$ - [F,L]# prevent access throughout the afternoonRewriteCond %{TIME_HOUR} ^(12|13|14|15)$RewriteRule ^.*$ - [F,L]Mask03/07/13, 05:20 PM
- Kích hoạt SSI: Khi sử dụng chế độ SSI, phần mở rộng các file trên server phải ở dạng .shtml thay vì .html. Điều này gây bất lợi cho các website đã thiết lập dạng .html, để tránh phải chuyển đổi lại định dạng mở rộng file trên server.
Có thể thêm nhiều dòng để server chấp nhận nhiều định dạng khác nhau.Ví dụ:
123AddHandler server-parsed .htmlAddHandler server-parsed .shtmlAddHandler server-parsed .htm - Chặn truy cập tới các file include trong file .php: Để tránh bị truy cập tới thư mục chứa các file .php, tạo file .htaccess với nội dung sau và includes là thư mục chứa file .php.
- Thủ thuật redirect: Đối với tất cả các loại redirect sử dụng mode_rewrite cần enable chế độ: RewriteEngine.
Redirect từ http://www.domain.com sang http://domain.com:
12345# permanently redirect from www domain to non-www domainRewriteEngine onOptions +FollowSymLinksRewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]Redirect từ một domain cũ sang domain mới:
Redirect String Variations sang một địa chỉ. Giả sử một request có chứa string: some-string, ta sẽ chuyển request này tới trang: http://some-string.com
12# redirect any variations of a specific character string to a specific addressRewriteRule ^some-string http://www.some-string.com [R]Một số phương pháp khác:
1234# map URL variations to the same directory on the same serverAliasMatch ^/director(y|ies) /www/docs/target# map URL variations to the same directory on a different serverRedirectMatch ^/[dD]irector(y|ies) http://domain.comRedirect một site đầu vào với trạng thái 301:
Redirect một file với trạng thái 301:
12# redirect a specific file via 301redirect 301 /current/currentfile.html http://www.newdomain.com/new/newfile.htmlRedirect một site qua một redirect liên tục:
Redirect một trang hoặc một thư mục với redirect liên tục:
123# redirect a page or directoryRedirect permanent old_file.html http://www.new-domain.com/new_file.htmlRedirect permanent /old_directory/ http://www.new-domain.com/new_directory/Redirect một file sử dụng RedirectMatch:
12# redirect a file using RedirectMatchRedirectMatch 301 ^.*$ http://www.domain.com/index.htmlKhi redirect các file, sử dụng rule Redirect với các file trong cùng domain, sử dụng rule RewriteRule cho bất cứ domain nào. rule RewriteRule mạnh hơn rule Redirect.
1234# redirect files directories and domains via RewriteRuleRewriteRule http://old-domain.com/old-file.html http://new-domain.com/new-file.htmlRewriteRule http://old-domain.com/old-dir/ http://new-domain.com/new-dir/RewriteRule http://old-domain.com/ http://new-domain.com/Rule này cho phép tất cả các visitor xem page thông qua sub-domain.
1234# send visitors to a subdomainRewriteCond %{HTTP_HOST} !^$RewriteCond %{HTTP_HOST} !^subdomain\.domain\.com$ [NC]RewriteRule ^/(.*)$ http://subdomain.domain.tld/$1 [L,R=301]Một số redirect khác:
12345# redirect query to Google searchOptions +FollowSymlinksRewriteEngine OnRewriteCond %{REQUEST_URI} .google\.php*RewriteRule ^(.*)$ ^http://www.google.com/search?q=$1 [R,NC,L]
123# deny request according to the request methodRewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD)$ [NC]RewriteRule ^.*$ - [F]
123# redirect uploads to a better placeRewriteCond %{REQUEST_METHOD} ^(PUT|POST)$ [NC]RewriteRule ^(.*)$ /cgi-bin/upload-processor.cgi?p=$1 [L,QSA]
12# seo friendly redirect for a single fileRedirect 301 /old-dir/old-file.html http://domain.com/new-dir/new-file.html12# redirects all files in dir directory with first letters xyzRedirectMatch 301 /dir/xyz(.*) http://domain.com/$112# seo friendly redirect entire site to a different domainRedirect 301 / http://different-domain.com
WEB FAQ > Email FAQ > Hỏi đáp Máy chủ > Hỏi đáp Linux Server
web faq | hỏi đáp web | hoi dap web | website faq | hỏi đáp website | hoi dap website | hướng dẫn sử dụng web | huong dan su dung web | huong dan web | hoi dap may chu | Hoi dap Linux Server