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

728x90

https://www.aleydasolis.com/htaccess-redirects-generator/subdomain-to-subdirectory/

728x90

I guess you are using Google Webmaster Tools and looking at duplicate content issues.

“Search Appearance” >> “HTML Improvements” >> “Duplicate title tags”

In a perfect SEO world you wouldn’t have any duplicate title tags, but the larger a site gets, more likely you’ll have duplicates.

 

I never use tags see Why Creating Lots of Categories and Tags is Anti-SEO.

If you delete all your tags a simple 301 redirect assuming you didn’t change your tags base on the WordPress permalinks page is:

RewriteEngine On
RewriteRule ^tag/(.*)$ http://example.com/ [R=301,L]

That would 301 redirect all tags (everything indexed under example.com/tag/ (all tags have the /tag/ slug by default) to the home page of example.com

 

If you have changed the tag base on the permalinks page change tag to what you use.

If you’ve been using the monthly archive widget another good 301 redirect rule is:

RewriteEngine On
RewriteRule ^2013/(.*)$ http://example.com/ [R=301,L]
RewriteRule ^2014/(.*)$ http://example.com/ [R=301,L]

 

This would 301 redirect 2013 and 2014 dated archives to home. Dated archives have no SEO value, waste link benefit, so if you delete the monthly archive widget and/or the calendar widget add the above. Go back as far as your site’s existed, one line for each year.

 

Always check you haven’t got anything important indexed under a domain with a site search

site:https://stallion-theme.co.uk/2014/

and

site:https://stallion-theme.co.uk/tag/

This one shows my categories:

site:https://stallion-theme.co.uk/responsive/

Under “Settings” >> “Permalinks” I’ve changed my Category base to “responsive”.

I wouldn’t want to use a 301 redirect rule which included “RewriteRule ^responsive/(.*)$ ….” since it would 301 redirect all my indexed categories which I want spidered and indexed!

Also check with Google Webmaster Tools and/or Google Analytics none of your tags generate search engine traffic. If you find a few are pulling in traffic either keep those tags or 301 redirect them to the most relevant webpage on the site (similar SERPs).

 

If you had a tag called “Awesome” that had SERPs and a category called “Awesome Stuff” a possible solution would be delete all the tags and use this:

RewriteEngine On
RewriteRule ^tag/awesome/(.*)$ http://example.com/category/awesome-stuff/ [R=301,L]
RewriteRule ^tag/(.*)$ http://example.com/ [R=301,L]

This would 301 redirect the Awesome tag to the Awesome Stuff category conserving SEO benefit. All other tags 301 to home.

This way you can conserve any ranking value from tags that have SERPs and redirect the rest to home.

In my experience tags tend not to generate traffic, especially single word tags. I’ve bought dozens of WordPress blogs with loads of tags I’ve deleted and never had to use the above code to conserve traffic. Though being a perfectionist I setup those types of 301 redirects anyway, I look for the most relevant webpage for something I’ve deleted because I can :-)

David

  More Comments by SEO Gold Services

  • SEO Tutorial Broken W3 Total Cache Plugin HTML Cache : The Meta Tags article worked as expected in FireFox (I was logged into WordPress), but didn’t work in Google Chrome (not logged into WordPress in …

    Continue Reading SEO Tutorial

  • SEO Tutorial Search Engine Traffic Recovery : Glad to hear your search engine traffic is recovering, the adult niche is a great niche to be in for high traffic, sex/porn etc… will …

    Continue Reading SEO Tutorial

  • SEO Tutorial .htaccess 301 Redirect Rules : Update: did a brain fart, the $1 format was correct. Think I need more sleep :-)

    so

    RewriteEngine On
    RewriteRule ^test/(.*)$ http://example.com/$1 [R=301,L]
    RewriteRule ^test2/(.*)$ http://example.com/$1 [R=301,L]

    Is correct.

    Bugger, I …

    Continue Reading SEO Tutorial

  • SEO Tutorial How to 301 Redirect a Directory : You add the .htaccess file 301 redirects to the domains you are redirecting FROM (domain1 and domain2 in your example) NOT the domain you are …

    Continue Reading SEO Tutorial

  • SEO Tutorial 301 Redirecting Duplicate Content : Easy SEO mistake to make.

    Good news is Google has probably taken the duplicate content as duplicate and ignored it, but you still should sort it …

    Continue Reading SEO Tutorial

728x90

Sometimes there are more than one WordPress installation is necessary, but your host might have limited MySQL Database allowance. Well the solution to that problem is having multiple WordPress installation in one database.

You must install WordPress manually in order for this to work. Most one click programs like Fantastic would not let you choose the prefix.

During the installation in your config.php phase, you want to change the prefix “wp_” in order to allow multiple installation. So it can be wpchris_ and the next one can be wpben_ you can use different prefix and use them to create as many installation as you need.

That is all you really need to do. This is also a security measure that you can take even if you don’t have to install multiple installation. By having a prefix, you are making the hacker work more to find out what is the SQL Address before he can hack.

+ Recent posts