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.
If you're just trying to redirect from the http version to https version of the same domain or the www to non-www (or vice versa), I have a tutorial on redirecting to the same domain.
How to redirect all pages to another domain using .htaccess:
- Set the nameservers of the old domain to somewhere you have cPanel hosting
- Go into cPanel and add the old domain
- Add a .htaccess file that 301 redirects all the internal pages to the new domain
- Add a blank index.php for extra safety.
If you already know the steps and are just looking for the htaccess code, it is:
RewriteEngine on RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]
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://".
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, you can 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
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:
RewriteEngine on RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
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 https://old.com/important-page/ the code will take important-page/ and tack it on to the end of https://www.newdomain.com/ so the visitor ends up on https://www.newdomain.com/important-page/
Be sure to change https://www.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 redirect a specific page to another domain 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 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.