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 301 redirect all pages of one domain to another domain using .htaccess:

  1. Set the nameservers of the originating domain to somewhere you have cPanel hosting
  2. Go into cPanel and add the originating 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

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 some spare hosting

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

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.

2 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

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

Leave a Comment