Drupal: How to Redirect Subdomain to Main Site Domain
Site was available through sub domain (for example, its test version), and you need to move it to server root. To avoid content duplicating (search engines don’t like this) you should canonize domain name (redirecting "www.example.com" → "example.com").
We will use the following ways to redirect subdomain to main site domains:
- Using of .htaccess file
- Using of Toggle WWW module
Using of .htaccess file
Find .htaccess file (it is at the end of site root) and add this:
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
After this code:
<IfModule mod_rewrite.c>
RewriteEngine on
How such redirecting works:
Using of Toggle WWW
An easy, PHP header redirect based way to redirect incoming links from http://www.example.com/some/deep/page to http://example.com/some/deep/page or vice-versa.
Intended for use on servers without Apache servers, hosts where .htaccess is not allowed, etc. Apache directives based redirects are faster than PHP based methods. It is a known caveat that it does not redirect to the correct URL when access is denied to a page; because drupal prompts for the access before the module is even loaded.
The advantages of this method:
- There is no waste of links weight (search engines pass weight to a new page)
- Old links still work