[.HTACCESS] Một số cách sử dụng .htaccess để cấu hình website - P3
- Kiểm tra URL
# automatically corect simple speling erors <IfModule mod_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.
# server custom error pages ErrorDocument 400 /errors/400.html ErrorDocument 401 /errors/401.html ErrorDocument 403 /errors/403.html ErrorDocument 404 /errors/404.html ErrorDocument 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.
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ị):
# redirect all visitors to alternate site but retain full access for you ErrorDocument 403 http://www.websitethuhai.com Order deny,allow Deny from all Allow from x.x.x.x
- Chặn truy cập tới file hay thư mục theo thời gian
# prevent access during the midnight hour RewriteCond %{TIME_HOUR} ^12$ RewriteRule ^.*$ - [F,L] # prevent access throughout the afternoon RewriteCond %{TIME_HOUR} ^(12|13|14|15)$ RewriteRule ^.*$ - [F,L] Mask 03/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.
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ụ:AddHandler server-parsed .html AddHandler server-parsed .shtml AddHandler 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.
## Enable Mod Rewrite, this is only required once in each .htaccess file RewriteEngine On RewriteBase / ## Test for access to includes directory RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /includes/ .*$ [NC] ## Test that file requested has php extension RewriteCond %{REQUEST_FILENAME} ^.+\.php$ ## Forbid Access RewriteRule .* - [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.
# initialize and enable rewrite engine RewriteEngine on
Redirect từ http://www.domain.com sang http://domain.com:# permanently redirect from www domain to non-www domain RewriteEngine on Options +FollowSymLinks RewriteCond %{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 from old domain to new domain RewriteEngine On RewriteRule ^(.*)$ 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.com# redirect any variations of a specific character string to a specific address RewriteRule ^some-string http://www.some-string.com [R]
Một số phương pháp khác:# map URL variations to the same directory on the same server AliasMatch ^/director(y|ies) /www/docs/target # map URL variations to the same directory on a different server RedirectMatch ^/[dD]irector(y|ies) http://domain.com
Redirect một site đầu vào với trạng thái 301:# redirect an entire site via 301 redirect 301 / http://www.domain.com/
Redirect một file với trạng thái 301:# redirect a specific file via 301 redirect 301 /current/currentfile.html http://www.newdomain.com/new/newfile.html
Redirect một site qua một redirect liên tục:# redirect an entire site via permanent redirect Redirect permanent / http://www.domain.com/
Redirect một trang hoặc một thư mục với redirect liên tục:# redirect a page or directory Redirect permanent old_file.html http://www.new-domain.com/new_file.html Redirect permanent /old_directory/ http://www.new-domain.com/new_directory/
Redirect một file sử dụng RedirectMatch:# redirect a file using RedirectMatch RedirectMatch 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.# redirect files directories and domains via RewriteRule RewriteRule http://old-domain.com/old-file.html http://new-domain.com/new-file.html RewriteRule 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.# send visitors to a subdomain RewriteCond %{HTTP_HOST} !^$ RewriteCond %{HTTP_HOST} !^subdomain\.domain\.com$ [NC] RewriteRule ^/(.*)$ http://subdomain.domain.tld/$1 [L,R=301]
Một số redirect khác:# rewrite only if the file is not found RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+)special\.html?$ cgi-bin/special/special-html/$1
# rewrite only if an image is not found RewriteCond %{REQUEST_FILENAME} !-f RewriteRule images/special/(.*).gif cgi-bin/special/mkgif?$1
# seo-friendly rewrite rules for various directories RewriteRule ^(.*)/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]
# broswer sniffing via htaccess environmental variables RewriteCond %{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]
# redirect query to Google search Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_URI} .google\.php* RewriteRule ^(.*)$ ^http://www.google.com/search?q=$1 [R,NC,L]
# deny request according to the request method RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD)$ [NC] RewriteRule ^.*$ - [F]
# redirect uploads to a better place RewriteCond %{REQUEST_METHOD} ^(PUT|POST)$ [NC] RewriteRule ^(.*)$ /cgi-bin/upload-processor.cgi?p=$1 [L,QSA]
# seo friendly redirect for a single file Redirect 301 /old-dir/old-file.html http://domain.com/new-dir/new-file.html
# redirects all files in dir directory with first letters xyz RedirectMatch 301 /dir/xyz(.*) http://domain.com/$1
# seo friendly redirect entire site to a different domain Redirect 301 / http://different-domain.com
- Nguồn : wiki.matbao.net
[.HTACCESS] Một số cách sử dụng .htaccess để cấu hình website - P3
Reviewed by Nguyen Nam Hong
on
6:30 PM
Rating:
