Simple Redirects with .htaccess

Anytime you migrate a web site, or even just move a single page to a new URL, redirects make sure that your users don’t get lost in the shuffle. Search engines also use redirects to aid in properly indexing your site’s content.

It is important to understand the distinction between the two most common types of redirects:

  • Permanent redirect — A permanent redirect, or 301 redirect, should be used anytime you permanently move a page, directory or website.
  • Temporary redirect — A temporary redirect, or 302 redirect, should be used if you want to temporarily point a user to another location.

If you are running an Apache server, then you can create a file named .htaccess in any directory to be able to locally override certain server configurations. It is very common for an .htaccess file to exist at the root directory of a website. You can use a very simple syntax within an .htaccess file to setup page, directory and site-wide redirects. While you can get quite advanced with URL redirects, we are going to get started with the simplest use cases.

There are three common use cases when setting up redirects:

  • Redirect a single page to a new page
  • Redirect a whole directory to a new directory
  • Redirect an entire site to a new site

Redirect Syntax

This is the basic syntax for redirects written using the mod_alias redirect directive in Apache:

  • Make sure you capitalize the R in Redirect or it won’t work. Everything is case sensitive.
  • The status is optional and is usually a number indicating the HTTP status code you want to deliver to the browser. You can use the word permanent in the place of 301, or temp in the place of 302. If not provided, then 302 will be used as the default.
  • The URL-path is required and is always a path relative to the site root, not the location of the .htaccess file.
  • The URL is required and is either a path relative to the site root, assuming the redirect is within the same site, or an absolute URL if the redirect points to another site.

Redirect a Single Page

Let’s start with a simple redirect where you want to point one page to another page:

As you can see, we are doing a 301 (permanent) redirect from a page on the current site, to another page on the same site.

If the new page is located at another domain, or even subdomain, then here is how you would write the redirect:

It is perfectly acceptable to use this method even if the page is on the same site. It never hurts to be more explicit and use an absolute URL.

Consider how the last example plays out with a few different URL variations:

# Target URL Destination URL
1 http://old.com/old-page.html http://www.new.com/new-page.html
2 http://old.com/old-page.html?q=21&id=902 http://www.new.com/new-page.html?q=21&id=902
3 http://new.com/old-page.html http://www.new.com/new-page.html
4 http://www.new.com/old-page.html http://www.new.com/new-page.html

Explanation:

  • Redirect #1 — Takes a page on an old domain to a new page on a new domain.
  • Redirect #2 — Does the same thing as the first redirect, but demonstrates that any URL GET parameters are passed along as part of the redirect.
  • Redirect #3 — An example of redirecting from the current domain to a subdomain.
  • Redirect #4 — Demonstrates a redirect that takes place on the same (sub)domain.

Redirect a Whole Directory

Here is an example of how you would redirect from one directory to another:

Again, let’s take a close look at how this example can play out:

# Target URL Destination URL
1 http://old.com/old-directory/ http://www.new.com/new-directory/
2 http://old.com/old-directory/page.html http://www.new.com/new-directory/page.html
3 http://old.com/old-directory/sub-directory/ http://www.new.com/new-directory/sub-directory/
4 http://old.com/old-directory/sub-directory/page.html http://www.new.com/new-directory/sub-directory/page.html
5 http://old.com/old-directory/?q=21&id=902 http://www.new.com/new-directory/?q=21&id=902

Explanation:

  • Redirect #1 — Takes a directory on an old domain to a new directory on a new domain.
  • Redirect #2 — Shows that any individual pages within the old directory are automatically redirected to the same location in the new directory.
  • Redirect #3 — Shows that any subdirectories within the old directory are automatically redirected to the same location within the new directory.
  • Redirect #4 — Illustrates the recursive nature of the redirect.
  • Redirect #5 — Does the same thing as the first redirect, but demonstrates that URL GET parameters are always passed along as part of the redirect.

The assumption with this type of redirect is that the contents of the directory are exactly the same on the destination URL as they previously were on the target URL. In other words, only the directory name has changed.

It is possible to combine a directory redirect and a few single page redirects, like this:

This example shows that order is important. When a redirect rule is hit, it happens immediately. The rest of the file is not processed for redirect rules. If our single page redirects are not hit, then the generic directory redirect will happen. This is the proper way of handling a redirect where the contents of the directory are exactly the same on the destination URL as they previously were on the target URL, except for the about.html and contact.html pages.

Redirect an Entire Website

Here is an example of how you would redirect an entire site:

As you can see, this redirect assumes that everything on the new site is in the same place as it was on the old site:

# Target URL Destination URL
1 http://old.com/ http://www.new.com/
2 http://old.com/?q=21&id=902 http://www.new.com/?q=21&id=902
3 http://old.com/page.html http://www.new.com/page.html
4 http://old.com/directory/ http://www.new.com/directory/
5 http://old.com/directory/page.html http://www.new.com/directory/page.html
6 http://old.com/directory/sub-directory/ http://www.new.com/directory/sub-directory/

Explanation:

  • Redirect #1 — The old root domain redirects to the new root domain.
  • Redirect #2 — GET URL parameters are always passed along.
  • Redirect #3 — Single pages are redirected to the same location on the new domain.
  • Redirect #4 — Directories are redirected to the same location on the new domain.
  • Redirect #5 — Child pages are redirected to the same location on the new domain.
  • Redirect #6 — Subdirectories are redirected to the same location on the new domain.

Let me know if you found these examples helpful!

2 thoughts on “Simple Redirects with .htaccess”

    1. Joe,

      A basic mod_alias redirect as described in this article isn’t really the way to go for a subdomain redirect. You’d need to use a mod_rewrite redirect. I’ve got an article describing how to redirect from a www subdomain to just the main domain and vice versa here: https://wpscholar.com/blog/www-redirects-with-htaccess/ I’d recommend that you check out that article. Essentially, you need that same type of redirect, but just adapt the code to point from the subdomain to the parent.

Leave a Reply

Your email address will not be published. Required fields are marked *