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:

Redirect [status] URL-path URL
  • 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:

Redirect 301 "/old-page.html" "/new-page.html"

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:

Redirect 301 "/old-page.html" ""

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


  • 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:

Redirect 301 "/old-directory" ""

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

# Target URL Destination URL


  • 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:

Redirect 301 "/old-directory/about.html" "/new-directory/about-us.html"
Redirect 301 "/old-directory/contact.html" "/new-directory/contact-us.html"
Redirect 301 "/old-directory" "/new-directory"

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:

Redirect 301 "/" ""

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


  • 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!


    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: 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 with about 30 .html pages to Joomla.
    Joomla has generated its own URL for each of the existing html page.

    Example: existing is now in the Joomla version.

    I have still retained all my html pages on

    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:”

    (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 is redirected the homepage of ? 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.


    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 (

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

      Redirect 301 “/landing/about.html” “””
      Redirect 301 “/products/specials” “””
      Redirect 301 “/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

    Unfortunately, this doesn’t work. What happens is that you get redirect to 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 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.

    1. Basically, you will want to use a more advanced redirect type (see On that post, there is a code block that shows how to redirect everything from an old domain to a new one assuming the URL structure is the same. In your case, you will want to edit that code to look like this:

      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
      RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
      RewriteRule (.+)$$1.html [R=301,L]

      This will not redirect the root domain but will convert all other URLs to have .html appended.

  5. Hi!

    I tried to redirect a page in my website to another page within the site. However, after setting it to the new destination, it directs me to the homepage and the old URL still appears.

    Hope I can get a reply.

  6. Hi, I would like to know what the best solution could be for my problem. I had to make the website on a new domain, as the old one had been compromised. After that I only transferred the old domain ( without hosting ) to my provider to keep it anyway as a redirect for the new website. But now I have the problem with all the old urls shown in Google that doesn’t nowhere, only the url of homepage works correctly. So i was told to buy the hosting again for the old website, so i can make the redirect work. So now I want to know the best solution ( and easiest): changing the index.php or how do I make a correct htaccess that redirects the whole website.
    thank you

  7. I need to redirect specific pages in my WP site, but none of these old WP pages targeted for 301 redirect work: page.html, page.htm, nor page.php They exist within a subdomain, and I want to redirect its pages to a new site in my domain. I’m editing the htaccess file in the subdomain.

    I can’t redirect using /page/ because there are other pages within the same directory I need to keep (e.g., ecommerce pages).

    I think I’m using the correct syntax.
    Redirect 301 /page.html

    Where are pages and posts filed in the WP hierarchy? Jeez. I can’t find any of my pages in the file structure of my WP site, so I can’t verify the exact pages I want to redirect, or their extensions.

    I prefer to redirect by htaccess but I suppose an alternative would be to reset the canonicals within WP. Advice? Thx.

  8. I’m trying to redirect a single url to a new destination, but only for a specific ISP, or even better, for a specific city. I do not want to effect anyone anywhere else that goes for that url. Can this be done on .htaccess?

    1. Redirecting via IP would work, assuming the user has a dedicated IP (see Specific cities aren’t really a reliable method. You’d have to take the IP address and use a service to determine the city, even then, my IP often shows that I’m in a different city than I actually am. Due to the processing of the IP that would have to take place to determine the city, you’d want to implement that using backend code, not in an .htaccess file.

  9. Hello, thank you for your article. I have a specific question, that I can’t seem to find the answer for.

    In WordPress, I want to redirect all non-existent (pages that are 404) pages within a URL path, to a specific url:

    RedirectMatch 301 ^/about/production-sales-figures$ https://%{ENV:SITE_FQDN}/about/production-sales-figures$1

    (I have the {ENV:SITE_FQDN} because this .htaccess is on 3 different environments)

    Is this possible? Can you help point me in the right direction?

    1. Well, detecting a 404 from .htaccess in WordPress isn’t possible. This is because the .htaccess file is going to route any requests for non-existing pages to the index.php file. From there, WordPress will do a lookup in the database to see if the current request matches any content in the database. If not, then WordPress returns a 404. Handling requests for non-existing pages in .htaccess would mean that WordPress would never load. As such, you’d have to handle the redirection login within WordPress itself. I’d recommend the following plugin: (you can redirect to a specific page, not just the homepage).

  10. great tutorial, mate! I finally understand .htaccess level redirects… well, at least the basic ones anyway, which is exactly what I need at the moment 🙂

    Really well explained….

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.