728x90

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" "http://www.new.com/new-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 URLDestination 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:

Redirect 301 "/old-directory" "http://www.new.com/new-directory"

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

#Target URLDestination 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:

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 "/" "http://www.new.com"

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

Share this:

Related Articles

WWW Redirects with .htaccessAugust 25, 2015

Redirect an Old Domain to a New Domain with .htaccessJanuary 29, 2013

Redirect Old Domain to New Domain via .htaccessAugust 28, 2010

Comments

  1. Joe

    September 4, 2016 at 8:54 am

    hello, how do i redirect all urls in a subdomain to the corresponding urls in the subdirectory
    i.e
    http://www.sub.site.com/many-site-urls
    to
    http://www.site.com/folder/many-site-urls

    Reply

    1. In reply to Joe

      Micah

      October 3, 2016 at 9:29 am

      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.

      Reply

  2. Singh

    February 14, 2017 at 7:39 pm

    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?

    Reply

    1. In reply to Singh

      Micah

      March 7, 2017 at 9:50 pm

      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.

      Reply

      1. In reply to Micah

        Singh

        March 8, 2017 at 1:21 am

        Thank you for your valued input…much appreciated.

  3. Kelly

    December 28, 2017 at 12:09 am

    Thank you for your article. I had a question and I truly appreciate any help. How could I permanently redirect from one subdomain to another via the .htaccess file? For example, to redirect http://old.domain.com to http://new.domain.com. I would like to keep the same URL structure, so another example would be http://old.domain.com/page_title would be 301 redirected to http://new.domain.com/page_title

    Reply

    1. In reply to Kelly

      Micah

      December 28, 2017 at 10:57 am

      Kelly,

      You may want to check out this article: https://wpscholar.com/blog/www-redirects-with-htaccess/

      That article is specific to www redirects, but the concept is the same. Your redirect would look something like this:


      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^old.domain.com [NC]
      RewriteRule (.*)$ http://new.domain.com/$1 [R=301,L]

      Reply

  4. Thomas

    January 17, 2018 at 3:29 pm

    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!

    Reply

    1. In reply to Thomas

      Micah

      January 24, 2018 at 12:10 pm

      Thanks for the question Thomas! Here is the answer to your question: https://wpscholar.com/blog/redirect-entire-website-except-wordpress-admin/

      Reply

  5. itroz

    January 23, 2018 at 10:25 am

    Thanks for awesome article

    Reply

  6. kim

    March 28, 2018 at 9:34 am

    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

    Reply

    1. In reply to kim

      Micah

      March 28, 2018 at 10:58 am

      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.

      Reply

  7. Dan Carr

    April 4, 2018 at 11:14 pm

    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.

    Reply

    1. In reply to Dan Carr

      Micah

      April 9, 2018 at 5:25 pm

      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.

      Reply

  8. Kapil

    August 5, 2018 at 6:28 pm

    Hi, I really need help…

    I want to change my entire WordPress website to plain HTML.
    So my URLs will like this
    Old: http://www.domain.com/pagename
    New http://www.domain.com/pagename.html

    How I can write a rule so my page old page redirect to new pages. I have around 200 pages.

    Plz help…

    Reply

    1. In reply to Kapil

      Micah

      August 7, 2018 at 12:13 pm

      Basically, you will want to use a more advanced redirect type (see https://wpscholar.com/blog/redirect-old-domain-to-new-domain-htaccess/). 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 (.+)$ http://www.newdomain.com/$1.html [R=301,L]

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

      Reply

  9. Lohlz

    February 25, 2019 at 9:17 pm

    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.

    Reply

    1. In reply to Lohlz

      Micah

      March 1, 2019 at 11:34 am

      Most likely, what you are running into is a specificity issue. Check out this article and then review your redirects to see if this might be the problem: https://wpscholar.com/blog/web-redirects-law-of-specificity/

      Reply

+ Recent posts