15

I've redirected my domain http://domain1.com to http://domain2.com using a 301 redirect.

Now i would like to redirect subdomain.domain1.com to domain2.com/folder when the user arrives on that url.

Can I do this in dns? Or in some other way?

Thank you for your help!

cmplieger
  • 7,155
  • 15
  • 55
  • 83

4 Answers4

17

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.

Karmic Coder
  • 17,569
  • 6
  • 32
  • 42
  • 4
    Just a note to add on to this correct answer. Some DNS management tools have tools for forwarding subdomains even though this isn't a pure DNS thing. If you're registrar has a DNS management tool, be sure to check as it may have some forwarding options built in. – Robert Waddell Dec 06 '13 at 19:12
  • 3
    `-1` this is not answering OP question fully. OK, we got that it's not possible, but where is possible way? – zur4ik Jul 10 '14 at 18:45
  • @zur4ik Apologies for being late to the party, but it says DNS doesn't allow it, it just isn't possible according to the standard. Any possible solution would be specific to the registrar and/or underlying server. – Luke Jan 05 '17 at 19:37
  • It answers "Can I do this in dns?" but not "Or in some other way?" – reducing activity Jul 17 '18 at 13:02
11

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  
reducing activity
  • 1,985
  • 2
  • 36
  • 64
Rajesh Durai
  • 119
  • 4
4

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. (don't 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.

0

You can mix DNS and Nginx to easily solve this trouble.

  1. On your DNS provider create an A record going to your server running Nginx.
  2. On your server on /etc/nginx/sites-enabled create a file containing this lines:
server {
  server_name subdomain.mydomain.com;
  rewrite ^ http://www.adifferentdomain.com$request_uri? permanent;
}
chefjuanpi
  • 1,570
  • 15
  • 27