.htaccess 🚂 Redirect a domain to another one

Share on email
Share on twitter
Share on linkedin
Share on facebook
Article Redirect a whole domain to another with the.htaccess file by @wpr0ck
Article Redirect a whole domain to another with the.htaccess file by @wpr0ck
Article Redirect a whole domain to another with the.htaccess file by @wpr0ck
Article Redirect a whole domain to another with the.htaccess file by @wpr0ck
Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email

You own several domain names for the same site? Here is a simple method to redirect all your secondary domain names to a primary domain name using the .htaccess file.

The codes present in this article are to be placed in the file named .htaccess at the root of all the secondary sites which must carry out the redirection. Remember to adapt this code with your information: protocol (http or https) and domain name.

.htaccess : Redirection of a whole domain to another one

This first code allows you to redirect all URLs to our main domain, here https://newdomain.com by keeping the path, but not the subdomain. That is, URLs with a subdomain such as www.olddomaine.com/chemin and those without sub-domains such as olddomain.com/chemin will be redirected to https://newdomaine.com/chemin. If you do not want to keep the path in the destination URL, simply delete $1.


   # Redirect all URL to https://newdomaine.com/$1
   RewriteEngine On
   RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]

.htaccess : Domain redirection with subdomain management

If we want a more precise redirection of the sub-domains, we will need to use the following code. Here, all URLs with the subdomain subdomain1 (of the form subdomain1.olddomain.com/chemin) will be redirected to subdomain1.newdomain.com/chemin, as well as for the subdomain subdomain2. All other URL forms will be redirected to https://newdomain.com/ (which will redirect the URLs of the form olddomain.com/chemin and www.olddomaine.com/chemin towards https://newdomaine.com/chemin).


    RewriteEngine On

    # Redirect subdomaine1.olddomain.com/* to https://subdomaine1.newdomaine.com/$1
    RewriteCond %{HTTP_HOST} ^subdomaine1.olddomain.com$
    RewriteRule (.*)$ http://subdomaine1.newdomain.com/$1 [R=301,L]

    # Redirect subdomaine2.olddomain.com/* to https://subdomaine2.newdomaine.com/$1
    RewriteCond %{HTTP_HOST} ^subdomaine2.olddomain.com$
    RewriteRule (.*)$ http://subdomaine2.newdomain.com/$1 [R=301,L]

    # Redirect all another to https://newdomaine.com/$1
    RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]

Julien MA Jacob
WordPress Developer
Julien MA JACOB - WordPress Developer
Passionate about the web and programming with always many projects in mind
  Discussion of the article
Subscribe to the newsletter
Notify to
guest
1 Commentary
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Manual

Hello, Thanks for this tutorial, It's strange because after the modification in the htaccess the redirection only works for the domain name. Do you have any idea why? Thanks to you;)