How do I redirect my site using a .htaccess file?

This document will explain how to create a .htaccess file to redirect your site or site content. This will not redirect any emails for your domains.

WARNING:

As a configuration file, .htaccess is very powerful.  Even the slightest syntax error (like a missing space) can result in your content not displaying correctly or at all.
Since .htaccess is a hidden system file please make sure your FTP client is configured to show hidden files.  This is usually an option in the program’s preferences/options.
  1. Create an empty text file using a text editor such as notepad, and save it as htaccess.txt.

    NOTE:

    The reason you should save the file as htaccess.txt is because many operating systems and FTP applications are unable to read or view .htaccess files by default. Once uploaded to the server you can rename the file to .htaccess.
  2. Edit the contents of the file.  Check the following examples:
    • Point an entire site to a different URL, such as domain.net redirected to domain.com:
      # This allows you to redirect your entire website to any other domain
      Redirect 301 / http://media.nilbd.tk/
    • Redirect index.html to a specific subfolder:
      # This allows you to redirect index.html to a specific subfolder
      Redirect /index.html http://mt-example.com/newdirectory/
      
    • Redirect an old file to a new file path:
      # Redirect old file path to new file path
      Redirect /olddirectory/oldfile.html http://mt-example.com/newdirectory/newfile.html
    • Redirect to a specific index page:
      # Provide Specific Index Page (Set the default handler)
      DirectoryIndex index.html
    • Redirect users to access the site without www:
      # To redirect all users to access the site WITHOUT the www. prefix,
      # (http://www.example.com/... will be redirected to http://example.com/...)
      # adapt and uncomment the following:
      
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^www\.mt-example\.com$ [NC]
      RewriteRule ^(.*)$ http://mt-example.com/$1 [L,R=301]
    • Redirect users to use www:
      # To redirect all users to access the site WITH the www. prefix,
      # (http://example.com/... will be redirected to http://www.example.com/...)
      # adapt and uncomment the following:
      
      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\.mt-example\.com$ [NC]
      RewriteRule ^(.*)$ http://www.mt-example.com/$1 [L,R=301]
      
    • Redirect viewers of your domain to use the secure version of your domain:
      # An easy to way to always redirect the user to secure connection (https://) can be accomplished with the following lines:
      RewriteEngine On
      RewriteCond %{SERVER_PORT} 80
      RewriteRule ^(.*)$ https://www.mt-example.com/$1 [R,L]
    • Redirect users to www for http:// and https://
      RewriteEngine On
      
      RewriteCond %{SERVER_PORT} 80
      RewriteCond %{HTTP_HOST} !^www\.mt-example\.net$ [NC]
      RewriteRule ^(.*)$ http://www.mt-example.net/$1 [L,R=301]
      
      RewriteCond %{SERVER_PORT} 443
      RewriteCond %{HTTP_HOST} !^www\.mt-example\.net$ [NC]
      RewriteRule ^(.*)$ https://www.mt-example.net/$1 [L,R=301]
    • Redirect users to use https:// for a particular folder:
      # In case you wish to force HTTPS for a particular folder you can use the following:
      RewriteEngine On
      RewriteCond %{SERVER_PORT} 80
      RewriteCond %{REQUEST_URI} somefolder
      RewriteRule ^(.*)$ https://www.mt-example.com/somefolder/$1 [R,L]
      
  3. Now upload the file to your root folder of your site and be sure to rename it to .htaccess without any filename extension.
  4. Make sure that your redirect functions properly.

NOTES:

0 comments: