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
Bình Luận Bài Viết