Search Our Database

How to Create Site Redirect Using “.htaccess”

Last updated on |
by

There are several ways to do a page redirection, one of the effective way to redirect a page or a website is by using .htaccess file.

.htaccess file can be found in the “public_html” folder. If the .htaccess file is still not created yet, you can simply create the file in your public_html folder.

Extra notes: Make sure that the ownership of the .htaccess file is set to the username.

Steps to redirect using .htaccess:-

  1. SSH to the server, or if you are doing it from the control panel, simply just go to the “file manager” from the control panel.
  2. Open the .htaccess file from the following path (this is directadmin path example, other control panel might have slight different path for the public_html) :-
    1. /home/<user>/domains/<domain-name>/public_html/.htaccess

  3. To redirect website to other website, paste the script in the .htaccess file:-
    1. RewriteEngine on
      RewriteCond %{HTTP_HOST} ^original-website-before-redirect.com.my [NC]
      RewriteRule ^(.*)$ http://www.new-website-to-be-redirected.com.my/$1 [L,R=301,NC]
  4. To redirect website to subdirectory, paste the script in the .htaccess file :-
    1. RewriteEngine on
      RewriteCond %{HTTP_HOST} example\.com [NC]
      RewriteCond %{REQUEST_URI} ^/$
      RewriteRule ^(.*)$ /subdirectory-name/$1 [L]
  5. To redirect website from http to https, paste the script in the .htaccess file :-
    1. RewriteEngine on
      RewriteCond %{SERVER_PORT} ^80$
      RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]