HackWare.ru

How to redirect subdomains to a new domain with the same subdomains

Contents

Task: redirect requests coming to subdomains to a new domain with the same subdomains using mod_rewrite in the .htaccess file settings. In this case, you need to save the request and query parameters (the requested page, etc.).

Quick solution

How to redirect requests coming to subdomains to a new domain with the same subdomains using mod_rewrite in the .htaccess file settings while preserving the query string

Subdomains with separate folders

Subdomains without folders


Task: redirect requests coming to subdomains to a new domain with the same subdomains using mod_rewrite in the .htaccess file settings. In this case, you need to save the request and query parameters (the requested page, etc.).

That is, the redirection should be as follows: https://rus.hackware.ru/?p=5900 → https://rus.new-site.com/?p=5900

Quick solution

In the .htaccess file, you need to create a rule

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.OLD-DOMAIN\.com$ [NC]
RewriteRule ^(.*)$ https://%1.NEW-DOMAIN.com/$1 [L,R=301]

If you also need to do a redirection for the root domain like https://OLD-DOMAIN.com/?p=5900 → https://NEW-DOMAIN.com/?p=5900, then you need to add another rule:

RewriteRule "(.+)" "https://NEW-DOMAIN.com/$1" [R=301,L]

In these rules you need to replace

  • OLD-DOMAIN\.com – your old domain from the subdomains of which the redirection is made (the backslash is not here by mistake, it is needed – it escapes the dot so that it acquires a literal value (and not the value “any character” as it happens by default)
  • NEW-DOMAIN.com – the new domain to the subdomains of which the redirection is made (the backslash is not needed here)

For example, you need the following redirection to occur

rus.hackware.ru → rus.new-site.com
eng.hackware.ru → eng.new-site.com
test.hackware.ru → test.new-site.com
hackware.ru → new-site.com

Then my rules in the .htaccess file are as follows:

RewriteEngine On
# Subdomain redirection
RewriteCond %{HTTP_HOST} ^(.*)\.hackware\.ru$ [NC]
RewriteRule ^(.*)$ https://%1.new-site.com/$1 [L,R=301]

# Root domain redirection
RewriteRule "(.+)" "https://new-site.com//$1" [R=301,L]

This results in redirection https://rus.hackware.ru/?p=100 → https://rus.new-site.com/?p=100

How to redirect requests coming to subdomains to a new domain with the same subdomains using mod_rewrite in the .htaccess file settings while preserving the query string

Now let's take a closer look at why the previous method might not have worked for you.

First of all, note that mod_rewrite rules can be configured in Apache configuration files, as well as in .htaccess. These methods are not completely equivalent, especially when it comes to setting up subdomains.

Here we are talking about how to redirect subdomains to a new domain with the same subdomains using mod_rewrite in the .htaccess file settings.

Subdomains with separate folders

From the web server's point of view, each subdomain is a separate virtual host that has (or at least can have) its own folder. This leads to an important conclusion:

Each subdomain with its own folder is configured individually, for this you need to change the .htaccess file located in the subdomain folder.

With this web server configuration (subdomains with separate folders), requests intended for subdomains are processed by files (including .htaccess) that are located in the subdomain folder, bypassing the files that are used to process root domain requests.

For example, there is a hackware.ru website with the following subdomains:

rus.hackware.ru
eng.hackware.ru
test.hackware.ru

The site files are located in the directory /home/host900000/hackware.ru/htdocs/www

And the subdomain files are located, respectively, in the following directories:

/home/host900000/hackware.ru/htdocs/rus
/home/host900000/hackware.ru/htdocs/eng
/home/host900000/hackware.ru/htdocs/test

If you redirect requests from subdomains to new addresses in the file /home/host900000/hackware.ru/htdocs/www/.htaccess, then this redirect will not work, since requests to subdomains will be sent directly to the folders /home/host900000/hackware.ru/htdocs/rus, /home/host900000/hackware.ru/htdocs/eng, and /home/host900000/hackware.ru/htdocs/test. For this reason, redirection should be configured in the files:

/home/host900000/hackware.ru/htdocs/rus/.htaccess
/home/host900000/hackware.ru/htdocs/eng/.htaccess
/home/host900000/hackware.ru/htdocs/test/.htaccess

So, the rule above:

RewriteEngine On
# Subdomain redirection
RewriteCond %{HTTP_HOST} ^(.*)\.hackware\.ru$ [NC]
RewriteRule ^(.*)$ https://%1.new-site.com/$1 [L,R=301]

I add to files

/home/host900000/hackware.ru/htdocs/rus/.htaccess
/home/host900000/hackware.ru/htdocs/eng/.htaccess
/home/host900000/hackware.ru/htdocs/test/.htaccess

This ensured the redirection of subdomains.

To redirect requests to the root domain, I used the rule:

RewriteEngine On
# Root domain redirection
RewriteRule "(.+)" "https://NEW-DOMAIN.com/$1" [R=301,L]

This rule is written in the file /home/host900000/hackware.ru/htdocs/www/.htaccess

Subdomains without folders

If you use subdomains as an alias to the domain, that is, all the files of the subdomains and the root domain are located in one folder, then in one .htaccess file of the root domain you need to write the above rule:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.OLD-DOMAIN\.com$ [NC]
RewriteRule ^(.*)$ https://%1.NEW-DOMAIN.com/$1 [L,R=301]

Since all requests (including for subdomains that are aliases) are sent to one folder, this rule will work for all such alias subdomains at once.

Update history

No update history yet.

Recommended:
↑