Follow

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

  • Applies to: All Service Types
    • Difficulty: Medium
    • Time Needed: 10
    • Tools Required: FTP, plain text editor

Last modified: March 6, 2020

Overview

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.

READ ME FIRST

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.

This article is provided as a courtesy. Installing, configuring, and troubleshooting third-party applications is outside the scope of support provided by (mt) Media Temple. Please take a moment to review the Statement of Support.

Instructions

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 htaccess.txt file. Below are some example redirects you can use. They are labeled in accordance to what actions they will perform.

301 (Permanent) Redirect:

Use a 301 redirect .htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

When adding the following to your website's .htaccess file, be sure to replace example.com with your own domain name.

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://example.com/

 

302 (Temporary) Redirect:

Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:

# This allows you to redirect your entire website to any other domain
Redirect 302 / http://example.com/

 

Redirect index.html to a specific subfolder:

# This allows you to redirect index.html to a specific subfolder
Redirect /index.html http://example.com/newdirectory/

 

Redirect an old directory to a new directory:

# Redirects example.com/old to example.com/new
RewriteRule ^old/(.*)$ /new/$1 [R=301,NC,L]

 

Redirect an old file to a new file path:

# Redirect old file path to new file path
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html

 

Redirect to a subfolder with URL masking:

# Show the content in example.com/folder2, but the URL appears as example.com/folder1
RewriteEngine On
RewriteRule ^folder1/?$ /folder2/

# To show the URL as just example.com RewriteEngine On
RewriteRule ^/?$ /folder2/

 

Redirect to a specific index page:

# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html

 

Redirect an error message:

Instead of prompting a 404 Not Found error page, the site will redirect to the homepage:

# Redirect 404 Error pages to the home page
ErrorDocument 404 http://example.com/

 

Redirect a non-existing page to index.php

# Redirect non-existing pages to index.php
Options +SymLinksIfOwnerMatch RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]

 

3. Upload this file and re-name it to .htaccess.

NOTE:

  • If using a text editor, be sure to save the file as plain text.
  • Paths to where you should save this file can be found in this article: System paths.
  • The definitive guide on Apache directives that can be used in .htaccess files can be found here: http://httpd.apache.org/docs/mod/core.html.

More powerful URL changes with mod_rewrite

If you need to make complex changes to the way your URL displays, you should visit Using .htaccess rewrite rules. You can do things like add "www" to the beginning of your URL, redirect all requests to a subfolder but keep the rest of the URL, etc.

See also

Was this article helpful?
1 out of 1 found this helpful

Comments