Hostwinds Tutorials

Search results for:


Table of Contents


How to redirect a subdomain with .htaccess

Redirect a Subdomain Using .htaccess

Tags: htaccess,  DNS 

How to redirect a subdomain with .htaccess

A subdomain is defined as a domain that is a part of an extension of the main domain. Subdomains can be anything from the commonly used www to something particular such as docs.google.com. Generally, you'd define where a subdomain points or uses as its webroot by setting its virtual host entry accordingly. However, you may find that you need to redirect your subdomain to a new URL in some cases. This article focuses on showing you how to redirect your subdomain to a different URL using a .htaccess file. If you have not created a .htaccess file or you're not sure how to edit a .htaccess, you can learn how to do both using the following article: Creating and editing a .htaccess file

How to redirect a subdomain with .htaccess

A common misconception is that a subdomain is somehow completely different than the main domain. This, however, is not true, and redirecting a subdomain is almost identical to redirecting the main non-subdomain. To redirect a subdomain, you can modify the following example code to fit your specific needs:

RewriteEngine On``RewriteCond %{HTTP_HOST} ^example.yourdomain.tld$ [NC]``RewriteRule ^(.*)$ https://www.anotherdomain.tld%{REQUEST_URI} [R=301,NC,L,QSA]

With this code in place in your .htaccess file, the subdomain should now redirect accordingly. If you're interested in learning how to redirect a main/primary domain, please feel free to check out our article that will show you how to accomplish this: Set a permanent (301) redirect using .htaccess

Written by Michael Brower  /  June 22, 2017