hosting – Redirect a subdomain to a specific url using DNS

hosting – Redirect a subdomain to a specific url using DNS

You can not do this with DNS. DNS is used to map domain names to IP address(es). It can not resolve a domain to a specific URI.

If you use an Apache server you can achieve this using .htaccess file. You can try adding following lines to the file and see whether it works.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www.)?subdomain.domain1.com$
RewriteRule ^(.*)$ http://www.domain2.net/subfolder$1

(or)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain.domain1.com$
RewriteRule ^(.*)$ www.domain2.net/subfolder$1  

hosting – Redirect a subdomain to a specific url using DNS

Rewriting internal DNS can be a pain, and I had exactly this problem so I wrote a system to solve it. Yes it uses the third party server to effectively do the redirection for you, but all you need is 2 DNS entries and it will work. Test it out. https://redir301.link/

Use requires the addition of 2 DNS records. One as a pointer to this service, and an additional record as the destination point. DNS RFCs prevent a CNAME record also having an associated TXT record so one needs to improvise.

To use, you need to set up the subomain to point to redir301.link. (dont forget the DOT at the end!)

 subdomain.foo.com      14400   IN  CNAME   redir301.link.

Then set the associated TXT record to the destination URL. TTL should be set to a MINIMUM of 300 (5 min).

 301.subdomain.foo.com  14400   IN  TXT <destination URL>

Destination URL can be any URL but the sub-subdomain needs to be 301 otherwise it will fail.

Leave a Reply

Your email address will not be published.