Jump to content
KUHM

301 Redirect from Several Domain Extensions to One Main Domain

Posted on - Updated

301 Redirect from Several Domain Extensions to One Main Domain

When the same site is served on both example.com and example.fr, you have two addresses showing exactly the same content: Google sees duplicates, picks the version to index on its own and splits the links between the two. A 301 redirect from several domain extensions to one main domain fixes the problem at its source: every URL on the secondary extension points, page by page, to its equivalent on the extension you have chosen.

I told you about the redirect that prevents a site from being indexed both with and without www. Multiple extensions (TLDs) are the same problem on a larger scale, and I ran into it at work with a client whose site answered identically on example.com and on example.fr.

Why a site on two extensions causes an indexing problem

Two extensions serving the same pages produce duplicate content across domains (or more, if the site also answers on .net, .eu or an old name). Google groups the duplicates and keeps a single version per page, but not necessarily the same one everywhere: some pages end up indexed on the .com, others on the .fr. The links the site receives are scattered between the two addresses, Search Console statistics are split across two properties, and visitors can land on either version depending on the link they followed.

A permanent redirect tells the search engine which address is the authoritative one. It passes the signals accumulated by the old URL on to the target page, and it stops you from leaving Google to decide for you.

Choose the main domain before writing a single rule

The choice of main domain is made once and for all, before touching the server. A few simple criteria:

  • The target audience: a site aimed mainly at France is better off keeping the .fr; an international site, the .com.
  • History: the domain with the most inbound links and the oldest rankings deserves to be kept.
  • The exact form of the URL: https, with or without www. Fixing this form avoids redirect chains, where exemple.com points to exemple.fr, which itself points to https://www.exemple.fr.

The rules below all target the same final address, https://www.exemple.fr, in a single hop.

The .htaccess code for an Apache server

On an Apache host with the mod_rewrite module, these lines placed at the top of the .htaccess file redirect every page of exemple.com, with or without www, to the same page on exemple.fr:

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

The condition tests the requested domain name, ignoring case (NC). The rule captures the requested path and appends it to the new domain using $1; R=301 makes the redirect permanent and L stops the processing of further rules. URL parameters are kept by default.

Redirecting all pages of the old domain to the new home page is tempting, but Google often treats such bulk redirects as not found pages: a page-by-page redirect preserves the value of each URL. For a page-by-page address change or the complete move of a site, the 301 redirect from an old URL to a new one, or for the whole site follows the same logic.

The equivalent on an Nginx server

Nginx does not read .htaccess files: the redirect is declared in the server configuration, in a block dedicated to the secondary domains:

server {
    server_name exemple.com www.exemple.com;
    return 301 https://www.exemple.fr$request_uri;
}

The $request_uri variable carries over the path and parameters of the requested page. For https requests on exemple.com, the block must also have a certificate valid for that domain: without one, the browser shows a security warning before it even receives the redirect.

In PHP, when the server is not accessible

Without access to the server configuration, a few lines of PHP do the same job. Place them at the very top of a file included by every page, before any output:

<?php
if ($_SERVER['HTTP_HOST'] !== 'www.exemple.fr') {
    header('Location: https://www.exemple.fr' . $_SERVER['REQUEST_URI'], true, 301);
    exit;
}

The third argument of header() sets the response code: without it, PHP sends a temporary redirect, which Google does not treat as a permanent move. The exit prevents the rest of the page from running.

Check the redirect and keep control of the old domains

The command curl -I http://exemple.com/une-page/ displays the response headers: you should see the 301 code and a Location line pointing to the same page on https://www.exemple.fr, with no intermediate step. Repeat the test on the home page, on a deep page and on a URL with parameters.

In Search Console, declaring both domains lets you follow the switch: the .com pages gradually drop out of the index while the .fr pages take over. The secondary extensions, meanwhile, must still be renewed every year: an abandoned domain can be bought by a third party, who then picks up the visitors and the links. The risks of losing your domain name, from expiry to disputes apply to these redirect addresses too.

Redirect or declare country versions?

A redirect only makes sense if both extensions show the same content. A .com offering a real English version and a .fr in French should not be redirected: keep each version and signal them to Google with hreflang tags, which indicate the language and country targeted. The same cleanup reflex applies within a single domain, for example for duplicate content between the root and index.php.

About the author

Cyril KUHM

Cyril KUHM

Head of MKH, a web agency near Strasbourg, and SEO consultant since 2007. On this blog I share free, hands-on advice to help your site rank better on Google.

Get in touch

Related posts

↑