+359 893 560 371 Support 24/7 · sales 9:00–18:00 All services operational
Domains

How to redirect a domain to another domain: 301 redirects explained

What a domain redirect is, the difference between 301, 302, 307 and 308, why forwarding with masking is a bad idea, how to redirect with .htaccess, nginx and Cloudflare, and how to change domains without losing Google rankings

The MaxBG team · 14 min read
How to redirect a domain to another domain: 301 redirects explained

You've renamed your business and registered a new domain. Or you own both the .com and the .co.uk and want them to lead to one website. Or you simply want your site to open the same way with and without www. In all three cases the answer is the same: a domain redirect.

In this article you'll learn what a domain redirect is, the difference between 301, 302, 307 and 308, why forwarding with masking is a bad idea, and how to redirect a domain with .htaccess, nginx and Cloudflare. You'll also see how to change your site's domain without losing your Google rankings and how to check that everything works.

In short

  • A domain redirect automatically sends visitors and search engines from one address to another.
  • For a permanent change, use a 301 (or 308). It tells Google to move rankings to the new address.
  • 302 and 307 are temporary and are not meant for a domain change.
  • A DNS record can't redirect on its own. The redirect is done by a web server: on your hosting, at your registrar or in Cloudflare.
  • When changing domains, redirect every old page to its new version, not everything to the home page, and keep the redirects for at least a year.

What is a domain redirect

A domain redirect (also called domain forwarding) is a setting that automatically takes anyone who opens one address to another. A visitor types old-domain.com, and the browser shows them new-domain.com, usually in a fraction of a second.

Technically, this is done with an HTTP response. The server for the old address doesn't return a page; it returns a 3xx status code and a Location header containing the new address. The browser reads it and opens the new address immediately. Search engines do the same, but they also note the type of redirect, because that decides where the Google rankings go.

A redirect can send a whole domain to another domain, one address to another within the same site, or a variant of the address, such as the non-www version, to the main one. The principle is the same. Only the rule changes.

Domain redirect – a visitor opens the old address and lands on the new site

When to redirect a domain

These are the most common situations where you'll need a redirect:

  • A new name or brand. The business has a new name, the new domain becomes the main one, and the old one has to lead to it so you don't lose customers and rankings.
  • Several domains for one brand. You've registered the .com and a country domain, a version with and without a hyphen, or a common misspelling of your name. They all lead to one main site.
  • A preferred version of the address. The site should open on one address only: with or without www, and always over HTTPS. The other variants redirect to it.
  • Merging websites. Two sites become one, and the old site's pages redirect to the closest matching pages on the new one.
  • Temporary situations. The domain points to a social media profile while the new site is being built, or to a campaign page for a limited time.

If you're still choosing a name, you can read more about how domains and extensions work in our article what is a domain name.

Types of redirect: 301, 302, 307 and 308

To a visitor, every redirect looks the same: they open one address and land on another. To search engines, however, the code matters, because it says whether the change is permanent or temporary.

CodeMeaningWhen to use it
301Permanent redirectChanging domains, choosing www or non-www, HTTP to HTTPS, moved pages
308Permanent redirect that keeps the request methodSame as 301, when forms and API requests must not change their method
302Temporary redirectA short campaign, a page under maintenance, tests
307Temporary redirect that keeps the request methodSame as 302, but stricter for forms and APIs

With 301 and 308, Google treats the new address as the replacement for the old one and passes the ranking signals to it. With 302 and 307, the search engine keeps treating the old address as the main one, because it expects it to come back. If you leave a 302 in place for months, Google may eventually treat it as permanent, but that is slow and unpredictable. So for a domain change, always use a 301.

There are also redirects done with a meta tag in the page (meta refresh) or with JavaScript. They work, but they are slower and harder for search engines to interpret. Use them only if you have no access to the server settings.

Forwarding with masking and why to avoid it

Many registrars offer domain forwarding with masking. The old domain stays in the address bar, while the other site's content is shown in a frame inside the page. It looks convenient, but it has serious drawbacks:

  • Search engines see an empty page with a frame and index the real site, not the masked domain. In practice, the masked domain doesn't exist in Google.
  • Many websites refuse to be shown inside someone else's frame, and the visitor then sees a blank page.
  • The address bar doesn't change as people navigate, so individual pages can't be shared.
  • It causes problems with HTTPS, login forms and payments.

If you want the second domain to show the same site, the right approach is a normal 301 redirect to the main domain, not masking.

Domain forwarding with masking – another site shown in a frame under the old domain

Can you redirect a domain with DNS alone

This is the most common misunderstanding. DNS translates a name into an IP address, but it can't send anyone to a different address. A CNAME record only says "this name is an alias for another name", and an A record points to an IP address. If you point the old domain to the new one with a CNAME, the browser will reach the new site's server, but with the old domain in the address bar. The site will either show an error or open on two addresses at once, which is duplicate content.

For a real redirect, you need a web server that returns a 301. It can be in one of three places: on your hosting (with .htaccess or in the nginx settings), at your domain registrar if it offers a forwarding service, or in a CDN service such as Cloudflare. DNS is only involved in pointing the old domain to the server that performs the redirect. We explain how the records work in our article what is DNS.

How to redirect a domain with .htaccess

If the site is on hosting with Apache or LiteSpeed, the easiest way is the .htaccess file in the old domain's root folder. If it doesn't exist, create it. The rule below redirects the whole old domain, with or without www, to the new one and keeps the path to every page:

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

The first line turns on the rewrite module. The second says the rule applies only to the old domain, with or without www. The third redirects every address to the same address on the new domain with a 301. So old-domain.com/services/ leads to new-domain.com/services/, not to the home page.

If you only need to redirect a single page, the rule is simpler: Redirect 301 /old-page/ https://new-domain.com/new-page/. Before you change .htaccess, save a copy. A single mistake in the file can bring the whole site down with a 500 error.

How to redirect a domain in nginx

On servers running nginx, a VPS for example, .htaccess doesn't work. The redirect is done with a separate server block for the old domain in the configuration:

server {
  listen 80;
  listen 443 ssl;
  server_name old-domain.com www.old-domain.com;
  # the ssl_certificate and ssl_certificate_key lines for the old domain go here
  return 301 https://new-domain.com$request_uri;
}

The $request_uri variable keeps the path and parameters of every request, so each page goes to the same place on the new domain. After the change, test the configuration with nginx -t and reload it with systemctl reload nginx. If these commands are new to you, see our guide to the essential Linux commands.

Redirecting a domain in nginx with return 301

Redirecting with Cloudflare

If the domain is on Cloudflare, you can redirect it without any hosting for it. This is handy for domains that only lead to your main site.

  1. Add a proxied DNS record. For a domain with no site of its own, an A record pointing to the placeholder address 192.0.2.1 is enough, as long as the orange cloud is on so traffic goes through Cloudflare.
  2. Create a redirect rule. Under Rules, choose Redirect Rules and set a condition for the old domain, the Dynamic type, an expression that keeps the path, and the 301 status code.
  3. Save and test. The rule takes effect within seconds, and Cloudflare also takes care of the SSL certificate for the old domain.

For many individual addresses, for example when moving an old site page by page, Cloudflare also has Bulk Redirects, where you upload a list of old and new addresses. You can read more about how such a network works in our article what is a CDN.

www or non-www, http or https

To search engines, http://domain.com, https://domain.com and https://www.domain.com are three different addresses. If your site opens on all of them, Google sees duplicate content and splits the signals between them. So choose one main version and redirect the others to it with a 301.

It doesn't matter whether you choose www or non-www. What matters is that there is only one. Here is an .htaccess rule that sends everything to HTTPS without www:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

One rule that makes both changes at once is better than two rules in a row. That way a visitor goes through one redirect, not two.

One preferred address – redirecting www and http to https without www

How to change domains without losing Google rankings

Changing your domain is the riskiest kind of redirect, because it affects the whole site. Done properly, rankings usually transfer within a few weeks. This is the sequence:

  1. List every address. Take them from your sitemap and from Google Search Console, including pages that other sites link to.
  2. Redirect each page to its new version. Use page-to-page 301s. Google treats redirecting everything to the home page as an error, and the rankings of individual pages are lost.
  3. Install an SSL certificate on the old domain too. Without one, browsers show a warning before the redirect even happens if someone opens the old address over https.
  4. Update internal links, the sitemap and canonical URLs to the new domain so there are no unnecessary redirects inside the site.
  5. Use the Change of Address tool in Google Search Console. Add and verify both domains and tell Google about the move.
  6. Keep the redirects for at least a year, ideally forever, and keep renewing the old domain. If it lapses, the redirects stop and someone else can buy the name.

The same approach applies when you move a blog from a subdomain into a folder on your main site. There's a worked example in our article what is a subdomain.

How to check that a redirect works

The quickest check is from the command line: curl -I https://old-domain.com/services/. In the response you should see HTTP/1.1 301 or HTTP/2 301 and a location: line with the exact new address. With curl -IL, you can see the whole chain up to the final page.

Without a command line, you can use your browser's developer tools. Open the Network tab, turn on Preserve log and load the old address. The first request should have a 301 status and lead straight to the final address.

Check a few things: that the code is 301, not 302, that every page leads to its own new version, that there's no chain of several redirects, and that all four variants of the old address work: with and without www, over http and https.

Checking a redirect – the 301 status and location header in the response

Common domain redirect mistakes

  • 302 instead of 301. A temporary redirect doesn't reliably pass on rankings. Many control panels set 302 by default, so check the code.
  • Everything to the home page. A visitor looking for a specific service lands on the home page, and Google loses the connection between old and new pages.
  • Redirect chains. The old address goes to HTTPS, then to www, then to the new domain. Every hop slows loading. Redirect straight to the final address.
  • Redirect loops. Two rules send the request back and forth, and the browser shows a "too many redirects" error. This usually happens when rules are mixed in .htaccess, in the control panel and in Cloudflare.
  • An expired old domain. A year or two later someone forgets to renew it, and every old link stops working.
  • Forgetting email. Redirecting the website doesn't redirect email. Messages sent to the old address go wherever the old domain's MX record points. Set up forwarding or an alias for the mailbox. We explain how business email works in our article how to create a business email.

Frequently asked questions

What is a domain redirect in simple terms?

It's a setting that automatically takes anyone who opens one domain to another. It's used when a business changes its name, when one brand owns several domains, or to make a site open on one address only.

Do you lose SEO when you redirect a domain?

With a proper page-to-page 301, the loss is minimal, and rankings usually recover within a few weeks. You lose out when a 302 is used, when everything goes to the home page, or when the redirects are removed too early.

What is the difference between a 301 and a 302?

A 301 is a permanent redirect that tells search engines to replace the old address with the new one. A 302 is temporary, and search engines keep treating the old address as the main one.

Can I redirect a domain without hosting?

Yes. Many registrars offer domain forwarding, and Cloudflare can do it for free with a redirect rule. All you need is control over the domain's DNS.

How long should I keep redirects in place?

At least a year, and ideally forever. External links to the old domain last for years, and each of them brings visitors and value only as long as the redirect works.

Why does my browser say there are too many redirects?

That's a loop: two rules send the request back and forth. Check that the same redirect isn't set in .htaccess, in the control panel and in Cloudflare at the same time, and that Cloudflare's SSL setting matches the server's.

Does email get redirected when I redirect the domain?

No. A website redirect only works for web addresses. Email depends on each domain's MX records and is set up separately.

Can I redirect a domain to a social media profile?

Yes, with a temporary 302 redirect while your site is being built. When the site is ready, remove the redirect so the domain can start ranking on its own.

Conclusion

A domain redirect is a simple setting with big consequences. Use a 301 for every permanent change, redirect page to page, avoid masking and chains, and keep both the redirects and the old domain for as long as possible. That way you can change your name, combine domains and tidy up the variants of your address without losing visitors or Google rankings.

Protect your name before someone else takes it. Register your main domain and its most important variants, and redirect them all to one website.

Check whether your domain is available · What is a domain name

AI assistant instant answers, 24/7
🍪 We use cookies

To keep MaxBG fast and secure, we use essential cookies. With your consent we also add Google tools (Analytics and Ads). They help us analyse visits and improve our hosting services, and the data is shared securely with Google. You can change your choice or learn more in our Cookie policy