Back to the blog Domény a hosting

How to Redirect a Domain: 301 vs. 302, DNS, .htaccess, and www

Learn when to use a permanent or temporary HTTP redirect, why DNS records alone do not redirect web pages, and how to plan domain and www changes. Includes Apache guidance, SEO considerations, and a practical checklist for testing the result.

Diagram showing a domain redirect, an HTTP status code, and the choice between www and non-www addresses

Decide what the redirect must accomplish

Before changing a domain setting, write down the exact starting addresses and the destination you want visitors to reach. A domain change may involve only the homepage, the entire site, or individual pages. Decide whether the destination should preserve each page’s path, such as sending an old product page to its replacement rather than sending every visitor to the new homepage. A clear mapping also makes it easier to recognize whether a rule is working as intended.

Next, decide whether the move is intended to be permanent or temporary. That choice affects the HTTP status code and the signal search engines receive. Do not choose a code simply because it is the first option in a hosting panel: confirm what the setting actually does and whether it applies to every relevant hostname and address. A control-panel label may describe a feature broadly, while the resulting response code determines what browsers and crawlers actually receive.

Make an inventory of the address variants in use: HTTP and HTTPS, the hostname with www, and the hostname without it. Also note any subdomains that should remain separate. This gives you a defined scope to check with your provider and helps prevent an incomplete change in which the homepage works but older page addresses do not. Include a few representative paths in the inventory, not just the site’s root address.

  • Record the preferred destination, including its protocol, hostname, and any required path.
  • Identify whether each move is permanent or temporary.
  • List the hostnames and page paths that must be covered.
Decide what the redirect must accomplish

Choose 301 or 302 according to the intended move

A 301 response indicates that a page has moved permanently. Use a permanent redirect when you are confident the move will not be reversed. HTTP 308 also indicates a permanent move, but this guide focuses on the common domain-migration question of whether a redirect is permanent or temporary. Confirm which codes your platform supports before you configure it, and verify the response after the change rather than inferring it from the name of a setting.

A 302 response indicates a temporary redirect. Google follows a temporary redirect, but does not treat it as a signal that the destination should become canonical. This can be useful when the original address is expected to return, such as during a temporary service interruption, and you want to avoid presenting the change as a permanent move. When the temporary condition ends, review the rule so it does not continue directing visitors away from the address you intend to use.

Neither code guarantees a particular ranking, traffic level, or indexing outcome. For a lasting site move, Google treats a permanent redirect as a signal that the destination should be canonical. That is one signal used in search processing, not a promise that every old address will be replaced immediately or that search visibility will remain unchanged. Choose the status code to match the real duration of the move, not to try to force a particular search result.

  • Choose 301 for a move you intend to keep in place.
  • Choose a temporary redirect such as 302 when the original address is expected to return.
  • Check the actual response code rather than relying only on a control-panel label.

Permanent or temporary?

A practical distinction between the intended duration of a move and the search signal associated with it.

DecisionPermanent moveTemporary move
Typical code301 or 308302
Use whenYou are confident the move will not be reversed.The original address is expected to return.
Google signalA permanent redirect is a signal that the destination should be canonical.Google follows it but does not use it as a canonicalization signal.
Choose 301 or 302 according to the intended move

Understand what DNS can and cannot do

DNS and HTTP redirects solve different parts of the journey. DNS helps a resolver find the service associated with a hostname. An HTTP redirect is a response from a web service that tells a browser or crawler to request another address. A DNS record does not itself send a 301 or 302 status, select a page path, or communicate that a web page has moved permanently. Keeping these roles separate helps you identify which service needs to be changed when a redirect is missing.

This distinction matters when a provider offers a feature called domain forwarding or URL forwarding. Such a service may receive a request and return an HTTP redirect, but the feature is not simply a DNS record. Check the provider’s documentation to learn whether it offers HTTP forwarding, what status code it returns, which hostnames it accepts, and whether it preserves paths. Find out whether it forwards only the homepage or also passes through the rest of the requested address; those behaviors can produce very different results during a site move.

DNS changes can also take time to be seen consistently because resolvers may cache answers. The record’s TTL, or time to live, controls how long a resolver may cache an answer before checking it again. That affects DNS changes; it does not make an HTTP redirect permanent or determine its SEO meaning. Avoid making changes at multiple layers without first identifying which one is responsible for the response.

  • Use DNS to direct a hostname to the appropriate service.
  • Use an HTTP redirect when a browser must be sent to a different address.
  • Ask whether a provider’s forwarding feature returns an HTTP status and preserves paths.

Set a preferred www or non-www hostname

A site may be reachable at both a bare hostname and its www variant. Choose one as the preferred hostname and redirect the other to it. Google’s guidance recognizes redirects as a way to send traffic from alternate versions to a chosen canonical address. Apache also recommends handling a non-canonical hostname in its virtual-host configuration where that approach is available. The important point is to make the choice consistently, so visitors and crawlers are not left with multiple equivalent versions.

For an Apache server, the documented approach is to place a Redirect directive in the virtual host for the hostname you do not want to use. Apache’s Redirect and RedirectMatch directives use a 302 response by default; specify permanent or 301 when the move is intended to be permanent. Confirm the target syntax and virtual-host arrangement against your server’s own configuration before applying a rule. The directive must be associated with the hostname that should receive the redirect, rather than accidentally affecting the preferred one.

Check that the chosen destination is consistent across the site. If the preferred version uses HTTPS and www, for example, avoid a sequence in which a request first changes from HTTP to HTTPS and then changes hostname in a second redirect. Every redirect adds another request, so a direct route to the final address is preferable when your server setup allows it. Test the alternate hostname as well as the preferred one, because a working site on one variant does not prove that the other variant is configured correctly.

  • Select one canonical hostname and route alternate hostnames to it.
  • Confirm the provider or server handles both hostnames.
  • Aim for a direct redirect to the final protocol and hostname.

Use .htaccess only for a compatible Apache setup

Apache can configure redirects in the server configuration or in an .htaccess file for a directory. Which location is appropriate depends on your hosting setup and what configuration access you have. A rule intended for Apache should not be copied into an unrelated server or hosting panel, and a provider may restrict which directives are permitted in .htaccess. If you are unsure where requests are handled, ask the host before adding rules in more than one place.

Apache’s official HTTP Server 2.4 reference, “Redirecting and Remapping with mod_rewrite,” documents an .htaccess example for redirecting HTTP requests to HTTPS. The example checks whether HTTPS is off, constructs the destination using the HTTPS scheme, the server’s configured name, and the captured request path, then returns a permanent 301 response and stops further rewrite processing. This describes the complete behavior of the documented example; it is not a copyable rule here because its destination depends on the server’s configured hostname.

That example is specifically an HTTP-to-HTTPS redirect. It does not, by itself, switch between www and non-www hostnames or establish that every site should use the same destination. Before using the documented pattern, confirm that the server has the required rewrite module and that its configured server name is the hostname you intend to serve. Consult the Apache reference and your hosting provider’s instructions for the exact configuration appropriate to your setup.

For a hostname-only change, use the server or provider’s documented method for that purpose rather than adding unrelated rewrite conditions by guesswork. If traffic passes through a proxy, do not base a security-sensitive redirect on X-Forwarded-Proto unless you control the upstream proxy and know it overwrites that header on every request. A header supplied by an untrusted request should not be treated as reliable evidence of the original connection’s protocol.

  • Confirm that the server is Apache and that the chosen configuration location is supported.
  • Treat Apache’s documented rewrite pattern as an HTTPS redirect, not a general domain-migration rule.
  • Verify that the configured server name matches the intended destination.
  • Avoid relying on proxy headers unless the proxy is under your control and configured to overwrite them.

Illustrative Apache redirect: test before making it permanent

For a domain move, this example belongs only in the old site’s document-root .htaccess file, with mod_rewrite enabled and the provider permitting the necessary overrides. Replace the example hostname with your different, configured destination, which must already work over HTTPS. Do not put the rule on a hostname that serves the destination as well, or you can create a loop. Back up the current configuration and obtain the administrator’s approval.

First test ordinary GET page requests with the temporary 302 response. Check the response code, final host, retained path and any query parameters; do not assume forms or other request methods behave identically. After the mapping is verified and the move is permanent, change R=302 to R=301. This is an illustrative rule, not a claim that it has been tested on your hosting.

  • RewriteEngine On
  • RewriteRule "^(.*)$" "https://www.example.org/$1" [R=302,L]

Plan the SEO and URL mapping

For a permanent move, map each important old address to its most relevant destination. If the site structure is unchanged, preserving the corresponding page path can help visitors and crawlers reach the replacement page directly. If a page has been removed or replaced by a different page, decide the appropriate destination rather than automatically sending every old address to the homepage. Review important page types individually, since a broad rule can send unrelated requests to a destination that does not answer the visitor’s need.

A permanent redirect is a canonicalization signal, and routing alternative hostnames to one preferred version helps consolidate access to the site. Keep internal links pointed directly at the chosen destination instead of sending visitors through redirects that your own site could avoid. This reduces unnecessary requests and avoids making ordinary navigation depend on an extra hop. Check links in menus, templates, and frequently used content so updates do not stop at the homepage alone.

A temporary redirect has a different purpose: it allows the original address to remain the intended address while visitors are sent elsewhere for a time. When the temporary condition ends, review the setup and remove or change the rule as appropriate. For a permanent move, keep the mapping aligned with the actual destination and avoid creating circular rules, where one address sends the browser back to the starting address. Also inspect chains in which one redirect leads to another, because each additional redirect creates another request.

  • Map old page addresses to relevant destination pages.
  • Update internal links to point directly to the preferred addresses.
  • Check that redirects do not form a loop or an unnecessary chain.

Apply the change and verify the result

Before editing DNS, a hosting control panel, or server configuration, save the current settings and write down the intended source-to-destination mapping. Confirm which service is responsible for answering requests for each hostname. If a registrar offers forwarding, a hosting server serves the website, and a proxy sits in front, establish where the HTTP response will actually be generated before changing more than one layer. This makes it easier to troubleshoot and to undo a change if the result is not what you intended.

After configuration, test representative addresses rather than only the homepage. Check the preferred hostname, its alternate, HTTP and HTTPS where applicable, and a page with a path. Confirm that each request reaches the intended final address and returns the intended permanent or temporary status. Use the provider’s documented testing method or an HTTP inspection tool; a browser displaying the destination alone does not tell you which status code was returned. Record the tested cases so you can repeat them if configuration changes later.

If the destination is wrong, a redirect loops, or a path disappears, pause and correct the rule before rolling it out further. Recheck after DNS changes have had time to be observed by resolvers, keeping in mind that caching is governed by TTL. Once the result is correct, update links you control and continue checking important old addresses for unexpected behavior. Test more than one page type when the site has distinct sections, because a rule that behaves correctly for one path may not cover another.

A useful final check is to start at each alternate version and follow the response through to the final destination. Confirm that the destination uses the intended protocol and hostname, that the relevant page path remains intact where it should, and that there are no avoidable intermediate steps.

  • Test both hostname variants and at least one representative page path.
  • Verify the final address and HTTP status, not just the browser’s displayed page.
  • Fix loops, unintended destinations, and avoidable redirect chains before considering the change complete.

A safe redirect workflow

Use this sequence to plan, configure, and check a domain redirect.

  1. 1
    Define the move

    Choose the destination, scope, and intended duration.

  2. 2
    Identify the configuration layer

    Confirm whether the redirect belongs in a provider feature or server configuration.

  3. 3
    Select the canonical address

    Choose the preferred hostname and protocol.

  4. 4
    Configure and inspect

    Apply the documented method and verify status, destination, and paths.

  5. 5
    Clean up

    Update internal links and correct chains or loops.

Frequently asked questions

When should I use a 301 redirect?

Use a 301 when you are confident the page has moved permanently. Google treats a permanent redirect as a signal that the destination should be canonical, but this does not guarantee a particular ranking or indexing result.

Is a DNS record the same as a domain redirect?

No. DNS helps resolvers find a service for a hostname; an HTTP redirect is a response that sends a browser or crawler to another address. A provider may offer a forwarding service that returns an HTTP redirect, so check its documentation rather than assuming a DNS record does this.

How do I redirect www to the non-www version?

Choose one hostname as canonical and configure the other to redirect to it. On Apache, the documented hostname approach is a Redirect directive in the virtual host for the non-canonical hostname. Confirm the configuration and status code supported by your server or hosting provider.

Can I use the .htaccess HTTPS example to redirect a domain?

Apache’s documented rewrite pattern described here redirects HTTP to HTTPS using the configured server name and the requested path. It does not, by itself, change the hostname from www to non-www or the reverse. Consult the Apache HTTP Server 2.4 reference and instructions for your own configuration, then test the resulting status and destination.

Sources and further reading 8