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!

14 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.

  1. Hello,

    I just migrated from an existing website on lets say http://www.example.com with about 30 .html pages to Joomla.
    Joomla has generated its own URL for each of the existing html page.

    Example: existing http://www.example.com/contact.html is now http://www.example.com/index.php/contact-us in the Joomla version.

    I have still retained all my html pages on http://www.example.com

    To use the existing html site I use ‘index.html’ & to switch to the joomla version I use the first page as ‘index.php’

    Not knowing much about redirects (301, 302) I was planning on re-doing the old html pages as follows:
    Example: ‘contact.html’ page I will delete all the content and links on this page and put a single link as follows:
    “This page has moved. For the new page visit Click here (link: http://www.example.com/index.php/contact-us)”

    (I was planning on doing this with all the existing 30 html pages).

    My main motivation for doing so is because some of the old html pages show up on the first page of Google search…which is crucial for me.

    My question: Is this the best way to do it…so that I do not lose the existing high ranks of my existing html pages in Google search?

    1. Singh,

      The approach you outlined would actually cause your rankings in Google to drop. The idea behind a 301 (Permanent) redirect is that your pages still exist, but at a different location. Google sees 301 redirects and will follow those and update their search index accordingly. With a 301, your rankings should be maintained. If you go the route you outlined, Google would think that you’ve deleted all the content on your pages and are linking off to another site. In that scenario, Google would index your new content, but your current rankings for those pages would be lost.

  2. Thank you for your examples. I have scenario that I don’t think is provided in your example. How would I redirect every page of an existing wordpress site (except for the admin) to redirect to the home page of a different domain. So for example, anyone going to any page on http://site-abc.com is redirected the homepage of http://site-xyz.com ? Only examples I see show pages redirecting to its counterpart on another domain directory, instead of going right to the root. Thanks again!

  3. Hi

    I want to redirect multiple pages from an old domain to a new domain with new paths. How do I do this. There is roughly 158 pages which I will have to manually input. Where do I start the 301 redirect on the HTACCESS code? If you can please give me an example with say 3 links that would be great.

    Thanks

    1. Kim,

      The code that goes in the .htaccess file can go anywhere as long as there aren’t other redirects before it that will take effect first. Note that the order of the redirect rules are important (https://wpscholar.com/blog/web-redirects-law-of-specificity/).

      This is a good example of how you might want to order your redirects:


      Redirect 301 "/landing/about.html" ""https://www.new.com/about-us.html"
      Redirect 301 "/products/specials" ""https://www.new.com/specials"
      Redirect 301 "/products" ""https://www.new.com/products"

      The first rule will redirect one page to its new page on the new domain. The second rule will redirect a whole directory to its new directory on the new domain. The last rule will redirect an entire directory (except the /product/specials/ directory and any of its children).

      If we reversed the last two rules, then the specials wouldn’t redirect properly because the products rule would match and the specials rule one would never be hit.

  4. I’m having some real problems with all of this.

    I have about 20 pages on an old site that I need to redirect to pages on a new site. The URL structure of the new site is totally different.

    Here’s one that I tried:
    Redirect 301 /dan-carr-gear-list/travel-gear https://dancarrphotography.com/gear/travel/

    Unfortunately, this doesn’t work. What happens is that you get redirect to https://dancarrphotography.com/gear/travel-gear/ for some reason.

    I just can’t figure it out.

    1. Dan,

      I’m not sure what the original domain is that you want to redirect from, so I can’t help you debug… but I’d recommend that you check out http://www.redirectcheck.com/ to see exactly what redirects are taking place. Also, keep in mind that your browser will cache 301 redirects. I recommend using 302 redirects when testing so you don’t have to keep clearing your browser cache every time you change a redirect.

Leave a Reply

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