I have been asked by my supervisor to look for a way to make the website's root domain access the sub-domain instead.


  • example.com is the root domain
  • test.example.com is the sub domain

Typing example.com should proceed instead to the test.example.com. Is this possible?

  • 4
    Do you want to see the subdomain in the browser's address bar? (ie. a redirection). Or, should the subdomain be "hidden" and you literally just want to serve the content from that subdomain?
    – MrWhite
    Nov 17, 2016 at 9:45

2 Answers 2


Indeed you can. For instance some do not realize a www. url is an actual sub-domain. So it is happening all the time.

You can go to your server settings and choose your sub-domain as the main domain name or add some code to create a redirect.

Many people use Apache servers for hosting websites and when you have used a domain name with traffic to it, a good idea is to use a 301 redirect in the .htaccess file located in the root folder of your website. The purpose is you are telling search engines, bookmarks etc, that hey we are the same and we moved here. That's the short version.

You would add some code like this to the htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://subdomain.example.com/$1 [L,R=301,NC]
  • 2
    I think this post could use some HTTPS implicit promotion :)
    – Pysis
    Nov 17, 2016 at 15:18

Alternatively if you run NGINX (you can't do .htaccess) create a server block for example.com

Have simply

server {
listen 80;
server_name www.example.com example.com;
rewrite ^ $scheme://example.example.com; 

server { 
listen 80;
server_name example.example.com;

  • 1
    The nginx common pitfall page recommends return 301 $scheme://example.example.com$request_uri; as a "better" solution instead of rewrite. Otherwise this should work. Nov 17, 2016 at 19:31
  • Good stuff, thanks for the advice I'll ensure to consider this when answering similar questions in future Nov 17, 2016 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.