Posted in: WordPress Tutorials

Subdomain Showing Website? Set up Redirection via .htaccess

Website subdomains, such as www and mail, are used to configure website and services for sites. However, an incorrectly setup subdomain can cause lots of trouble.

In this article, we share how to redirect your subdomains to the main website using .htaccess rules for Apache web servers.

The following instructions are applicable for PHP / Apache web hosting servers only. It does not apply for Microsoft IIS web servers, which use web.config file.

When a new web hosting account is created using WHM (Web Hosting Manager) and cPanel, there are many subdomain set up automatically. One of the most important subdomains is the www subdomain.

Some of the default sub-domains with cPanel are mail, webmail, whm and cpanel. So, if your website domain is example.com, you will have the following subdomains added automatically.

  • www.example.com [website]
  • mail.example.com [email subdomain as a CNAME alias]
  • webmail.example.com [email client]
  • cpanel.example.com [web hosting cPanel]
  • whm.example.com [web hosting manager]

Issue: duplicate website on mail subdomain

Many people using cPanel-based web hosting find their website to be appearing as a duplicate on the mail subdomains.

As the mail subdomain is a CNAME entry in the DNS Zone Files, the mail subdomain is just another alias for the main site domain. When the server is not set up properly, anyone typing the mail subdomain in browser will get to see the website, with mail subdomain as the URL.

The accessibility of website on mail subdomain creates the problem.

  1. Mail subdomains with website create usability issue and confusion
  2. Duplicate content for SEO as Google will consider the main site and the mail subdomain site as two different websites
  3. Google and other search engines may index the subdomain site

Here’s a problem found in a web master support forum.

My ‘mail’ subdomain now shows in search results
I use cPanel with my hosting provider. The ‘mail’ or ‘webmail’ subdomains are automatically created with every domain I set up. I’m now seeing links to content that includes my ‘mail’ subdomain in the URL. I’m assuming that Google Search sees these pages as duplicates or as non-canonical(-ly) correct, and effects the error counts I see in the search console results, and I would presume could penalize my rankings.

Redirect all subdomains of domain

If you have a website hosted on the naked domain, without any subdomain such as www, you can set up .htaccess rules to redirect all subdomains to the desired URL.

#Redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+).example.com$ [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]

Replace the domain name with your website domain, and add the correct format based on whether your website is served on http or https (SSL).

Don’t use the code above if you plan to use www (or any other subdomain).

Redirect mail subdomains of domain

If any subdomain (including, mail.example.com) of your website is resulting in a duplicate of your website, the server is configured to handle HTTP requests from the mail subdomain to the same virtual host which handles your primary website.

You can set up redirection for mail.example.com to your main website url http://example.com using the code below in your .htaccess file.

#Redirect mail subdomain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mail.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R,L]

Another solution for the problem is to make mail subdomain restricted and show a 403 (forbidden) error to web browsers and visitors.

#Make mail subdomain forbidden
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mail.
RewriteRule . - [F,L]

Where to find .htaccess file

You can find the .htaccess file in the public_html or root folder of your website hosting server.

If you are using cPanel File Manager, you will have to enable the preference to show hidden files, or files where name stars with dot. You can choose to show hidden files in the dialog box that shows up before file manager opens. Alternatively, you can click on the settings / preferences option (the gear icon at the top right) and enable showing hidden files.

Copy the above code at the beginning of the .htaccess file. Check if all is working well. Make sure to keep a backup of the .htaccess file so that you can revert to old rules in case of any configuration issue.