How To Redirect All Pages From One Domain To Another With .htaccess

Redirecting pages or websites tends not to be something most web designers deal with on a regular basis.

What sometimes happens is you change your domain and keep the pages the same on the new domain.

You don't want to redirect all the pages of the old domain to the homepage of the new domain, you want them to go to the correct internal pages.

I've had this exact question from three different people in the last week, so I figured it deserved a tutorial.

How to redirect all pages to another domain using .htaccess:

  1. Set the nameservers of the old domain to somewhere you have cPanel hosting
  2. Go into cPanel and add the old domain
  3. Add a .htaccess file that 301 redirects all the internal pages to the new domain
  4. Add a blank index.php for extra safety.

If you already know the steps and are just looking for the htaccess code, it is:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
</IfModule>

In this example, I have a domain, inteliwolf.com (notice the single "L") that I want to redirect to intelliwolf.com (the site you're reading this on, with the two "L"s).

Inteliwolf.com is registered at GoDaddy and I have some free space on a HostGator host that I'll use.

The steps are the same, no matter which registrar and host you use. The screens will just look a bit different.

At your domain registrar, go into the DNS Manager for the old domain.

You can try Domain Forwarding if that works. I've found this takes ages to actually redirect and it has issues with redirecting internal pages.

We're not going to forward the domain in this tutorial. This section is just in case it works for you.

If you did want to use Domain Forwarding, you put your destination domain in the Forward To box and select "https://" or "http://".

Check Update my nameservers and DNS settings to support this change and leave everything else as default.

Click save, and wait for a few hours for the DNS to propagate.

If this works for you, stop the tutorial now.

We'll proceed with the tutorial as if you haven't done the domain forwarding.

Point the nameservers of the old domain to your host

You might have space to add an addon domain at the same host you use for the destination domain. That's typically where I prefer to host this redirect, just to keep things in one spot.

It's perfectly fine for you to host the old domain where it was or at another host.

The main thing is it has cPanel. If it's a Windows IIS server, Parallels or something else, the implementation will be different from what's shown here.

In DNS Management, click "Change" next to the nameservers.

Switch from Default nameservers to Custom nameservers.

When you setup the host, you would've received an email listing the two or more nameservers for that hosting account.

If you can't find the email, you might be able to find it in the sidebar of your cPanel. It will probably be listed under Primary and Secondary Nameservers.

For the purpose of this tutorial, I'll be using

ns8439.hostgator.com
ns8440.hostgator.com

There will usually be two nameservers, sometimes three or more.

Enter the nameservers at the domain registrar as shown and click "Save".

Add the old domain to the host

While the DNS is propagating, go to where you'll be hosting the old domain.

Assuming it's not already setup at that host, and that it's not the primary domain, set it up as an Addon Domain by going to Addon Domains.

Enter the old domain in New Domain Name. Everything else should be fine as default.

There's no reason to create a new FTP account for this domain.

Click "Add Domain".

If everything's good, you'll see a success message.

Add the .htaccess file to the old domain

Now to add the code that will actually do the redirecting.

Go to File Manager.

Navigate to the folder you added for the old domain in the Addon Domains step.

HostGator already had a .htaccess file in the folder, so you just select it and click Edit.

If you don't have a .htaccess file there, first check you can see hidden files, by going to Settings (in File Manager) and checking Show Hidden Files (dotfiles).

If you still can't see a .htaccess file in the home folder for the domain, click "+ File" and create one.

In .htaccess, add the following code:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
</IfModule>

The key line is RewriteRule.

The line says using ^(.*)$ "take the whole page, not including the domain..."

"... and stick it where the $1 is".

So if someone goes to http://old.com/important-page/ the code will take important-page/ and tack it on to the end of http://newdomain.com/ so the visitor ends up on http://newdomain.com/important-page/

Be sure to change http://newdomain.com in the code to your new domain.

As you can see, I've changed mine to intelliwolf.com.

Click "Save Changes".

Add blank index.php file

For an added layer of security, I don't like to leave a domain empty like this.

You should always add a file like index.html or index.php even if it's blank.

When you do that, if something goes wrong with the .htaccess redirect and it doesn't end up redirecting, anyone going to the old domain will just see a blank page, rather than a listing of all the files in there.

Click "+ File" to add a new file.

Call the new file index.php and click "Create New File".

You don't have to add anything into this file. If everything goes well, no one will ever see it.

It may take a few hours for the redirect to start working.

The domain registrar has to tell all the other DNS phonebooks that your old domain is now located at the new host.

Once that's done, you probably won't need to touch it again. There's nothing to update and this code has worked for the 20-odd years I've been building websites.

How to do a specific page redirect with .htaccess

If you want to redirect just one page from the old domain to somewhere different, you just need one line:

Redirect 301 /old-url/ http://newdomain.com/new-url/

Replace /old-url/ with whatever comes after the domain for the URL you want to redirect from.

Then put the full URL of the destination page.

You can use this independently of the domain redirect I showed at the start.

If, instead of redirecting the entire site, you literally just wanted to redirect one URL from the old domain (maybe it was on your business cards), then you could just put this line in the .htaccess instead of the RewriteRule line.

21 thoughts on “How To Redirect All Pages From One Domain To Another With .htaccess”

  1. Hello,
    Thank you very much for the tutorial. Finally I maybe found something I'm looking for. I have an other issue.
    I had a website olddomain.be and it was hosted on Cloudways. When I asked how change a domain name site structure is the same, I just had to change the primary domain name and for redirects I had to add as extra domains. So at this point I did this and I have one website two domain names. A week later I change the hosting provider. I add my olddomain.be as alias for redirects and it only redirects homepage.
    They do not have any addon cpanel. My olddomain.be is also at the same host and the same ip address as my new one. So if I land on olddomain.be (homepage) it redirects me immediately to newdomain.be.
    But the intern links are accessible from both domains so for example my website about us page is reachble from olddomain.be and also from newdomain.be. Only when I click further in the website it redirects me to the newdomain. But landing pages don't redirect. so if I land on olddomain.be/about-us it will stay like this it does not redirect.

    The problem is I have to domains and olddomain's .htaccess is actually the one of the new domain so there is only one website. But I really would like to redirect all the old domain intern URLs to the equivalent newdomain url's like you describing here. Could you give me advice.
    If I will transfer my olddomain.be to a hosting with cpanel, but actually it's (just a domain without website). Create .htaccess file there like you describe and add this code will. Will it work?

    With kind regards,
    Amelia

    Reply
    • If you don't have the option to addon extra domains to your host, you might need to upgrade your hosting or get additional hosting for this. You can even get some hosting for like $10 per year. I wouldn't run a website on that, but it's perfectly fine for running redirects.

      Reply
  2. I want the old domain and all internal pages to be redirected to the new domain only (homepage only) irrespective of which internal page or folder it came from in the old domain

    Reply
    • You can either do the redirect from your domain registrar, if it allows it, or use the code:

      RewriteRule ^(.*)$ http://newdomain.com/ [R=301]

      Replace "newdomain.com" with your new domain.

      The same would apply if you wanted to redirect everything to a specific internal page, maybe one with information about what happened to the old site. You would just put that full URL where I have http://newdomain.com/ above.

      Reply
  3. This code not redirecting one domain to other domain same page. It was redirecting in direct my home page.
    Example:
    Olddomain . com/ about .html

    It will redirecting me.

    Newdomain . net

    What can i do now?

    Reply
    • Did you have the $1 at the end? That's the bit that says "take everything from the end of olddomain and stick it at the end of newdomain".

      If you're using the code as above, perhaps your host has an issue with mod_rewrite or symlinks. Best to check that with them.

      Reply
      • Hello Mike

        I redirected olddomain.com to my newdomain.com and it has been 3 weeks and olddomain.com links are not appearing in Google Search Console. Is this related to the redirection or something else? I used .htaccess method

        Reply
        • I found it can take 6 months or more for the links to switch over in Google Search Console, even if they're being counted.

  4. Hello Mike,
    Thanks for answering on my previous comment. I did everything as you recommended and added at the beginning of htaccess the individual url redirects from old domain to new domain. But it's not working
    I did this

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    Rewriterule ^fr/product-tag/$ https://www.newdomain.be/fr/shop/ [R=301,L]
    (with this rewriterule I wanted that all othe url's like fr/product-tag/page/1 or fr/product-tag/page/2 and also possible paramteres all would go to newdomain.be/fr/shop, all paramters and everything coming after/product-tag/ would be ignored. But it does not happen. What am I doing wrong?

    And at the end I added this

    RewriteEngine on
    RewriteRule ^(.*)$ http://www.example.be/$1 [R=301,L]

    Reply
  5. ''
    RewriteEngine on
    RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
    ''

    Perfect!
    However, I don't want olddomain.com to be redirected, I only want olddomain.com/x redirected.

    See what I want at wikipedia.org/x redirected to en.wikipedia.org but wikipedia.org is NOT redirect. That's what I want.

    Any solution?

    Reply
    • Hey Andrew

      You need to add an exception. To do what you're after, try this:

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{REQUEST_URI} !^/$
      RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
      </IfModule>

      The "RewriteCond" line says "only run the rule on pages that start with a slash after the domain".

      That configuration worked for me to redirect everything except the home page. Hope that helps.

      Reply
  6. Hello there.

    Please how do I make all images and files from a remote respository like wikimedia commons to be canonical to my website using .htaccess file or another means you may recommend?

    Thank you.

    Reply
    • Hi Andrew

      You can't set the canonical tags in htaccess, only in HTML. Canonical only applies to HTML code, not to images. And even if you could set them for images, the canonical place for wikimedia images is on wikimedia and they would have to set it.

      Reply
  7. Hello Mike

    The article worked perfectly for me but I'm wondering if it's possible to exclude a specific folder from the rule.

    Basically, I have thousands of form submissions stored in wp-content/uploads. After the redirect, anytime i try to access olddomain.com/wp-content/uploads/gravity_forms/form4 it redirects it to newdomain.com/wp-content/uploads/gravity_forms/form4.

    Is there a way to fix this?

    Thank you very much for your help.

    Reply
    • Hey Omar. Add this line before the RewriteRule:

      RewriteCond %{REQUEST_URL} !^/wp-content/uploads/gravity_forms/.*$

      You might need to flush the cache if the browser has saved the redirect.

      Reply
  8. What would be the best way if the pages on my new domain are named differently? Sure, I have to redirect all pages individually but how would this work for the homepage?
    Thanks.

    Reply
    • If you're going WordPress to WordPress, I'd use the code from the article to redirect from the old site to all the pages on the new site, then use a plugin like Redirection to redirect the old pages to the new pages.

      Reply

Leave a Comment