kapan perlu redirect ?
Ketika anda punya broken link yang ingin supaya link nya dapat dipakai lagi, semisal karena sudah terindex oleh google. Bisa juga karena web anda sedang maintenance sementara sehingga redirect kehalaman maintenance agar pengunjung dapat info.
Okey… disini saya anggap temen2 sudah tau apa itu file htaccess, karena kita akan langsung kepada intinya. Bagi yang belum mengetahui tentang htaccess bisa baca-baca disini.
persiapan
pertama kita buat dulu file .htaccess lalu taruh di root directory web kita atau folder yang membutuhkan, misal kalau menggunakan wordpress file .htaccess sejajar dengan folder wp-content wp-include dll. lalu file .htaccess itu akan kita isi dengan script untuk redirect. berikut macam-macam cara redirect dengan file .htaccess
301 (Permanent) Redirect
Redirect seluruh URL website ke URL yang berbeda secara permanent. Contohnya kita redirect ke domain qodrbee.com
# This allows you to redirect your entire website to any other domain Redirect 301 / http://qodrbee.com/
302 (Temporary) Redirect
Redirect seluruh URL website ke URL yang berbeda secara sementara. Ini berguna untuk tujuan SEO ketika anda punya landing page sementara dan berencana nanti akan kembali ke landing page utama. Contohnya kita redirect ke domain qodrbee.com
# This allows you to redirect your entire website to any other domain Redirect 302 / http://qodrbee.com/
Redirect index.html ke specific subfolder
# This allows you to redirect index.html to a specific subfolder Redirect /index.html http://qodrbee.com/newdirectory/
Redirect file lama ke file baru
# Redirect old file path to new file path Redirect /olddirectory/oldfile.html http://qodrbee.com/newdirectory/newfile.html
Redirect ke specific index page
# Provide Specific Index Page (Set the default handler) DirectoryIndex index.html
Redirect non-www ke www .htaccess
RewriteCond %{HTTP_HOST} ^qodrbee.com [NC] RewriteRule ^(.*)$ http://www.qodrbee.com/$1 [L,R=301]
Redirect www ke non-www .htaccess
RewriteCond %{HTTP_HOST} ^www.domainanda.com [NC] RewriteRule ^(.*)$ http://domainanda.com/$1 [L,R=301]
Redirect seluruh website ke website lain
RewriteCond %{HTTP_HOST} ^domainlama.com [NC,OR] RewriteCond %{HTTP_HOST} ^www.domainlama.com [NC] RewriteRule ^(.*)$ http://domainbaru.com/$1 [L,R=301,NC]
Redirect Url Direktori ke Halaman Utama
RedirectMatch 301 /blog/(.*) https://wwqodrbeew.idsysadmin.com/$1 RedirectMatch ^/$ https://www.qodrbee.com/blog
selain dengan file .htaccess redirect juga bisa dilakukan dengan setting config nginx