- Kiểm tra URL1234
# automatically corect simple speling erors<IfModulemod_speling.c>CheckSpelling On</IfModule> - 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.1
RemoveHandler 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ị):12345
# redirect all visitors to alternate site but retain full access for youErrorDocument 403 http://www.websitethuhai.comOrder deny,allowDeny from allAllow from x.x.x.x - Chặn truy cập tới file hay thư mục theo thời gian12345678
# 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.1
AddHandler server-parsed .html
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.123456789
## Enable Mod Rewrite, this is only required once in each .htaccess fileRewriteEngine OnRewriteBase /## Test for access to includes directoryRewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /includes/ .*$ [NC]## Test that file requested has php extensionRewriteCond %{REQUEST_FILENAME} ^.+\.php$## Forbid AccessRewriteRule .* - [F,NS,L] - 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.12
# initialize and enable rewrite engineRewriteEngine on
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:123# redirect from old domain to new domainRewriteEngine OnRewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]
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.com12# 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.com
Redirect một site đầu vào với trạng thái 301:12# redirect an entire site via 301redirect 301 / http://www.domain.com/
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.html
Redirect một site qua một redirect liên tục:12# redirect an entire site via permanent redirectRedirect permanent / http://www.domain.com/
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.html
Khi 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/ 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:123# rewrite only if the file is not foundRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.+)special\.html?$ cgi-bin/special/special-html/$1123# rewrite only if an image is not foundRewriteCond %{REQUEST_FILENAME} !-fRewriteRule images/special/(.*).gif cgi-bin/special/mkgif?$112345# seo-friendly rewrite rules for various directoriesRewriteRule ^(.*)/aud/(.*)$ $1/audio-files/$2 [L,R=301]RewriteRule ^(.*)/img/(.*)$ $1/image-files/$2 [L,R=301]RewriteRule ^(.*)/fla/(.*)$ $1/flash-files/$2 [L,R=301]RewriteRule ^(.*)/vid/(.*)$ $1/video-files/$2 [L,R=301]123456# broswer sniffing via htaccess environmental variablesRewriteCond %{HTTP_USER_AGENT} ^Mozilla.*RewriteRule ^/$ /index-for-mozilla.html [L]RewriteCond %{HTTP_USER_AGENT} ^Lynx.*RewriteRule ^/$ /index-for-lynx.html [L]RewriteRule ^/$ /index-for-all-others.html [L]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
