2

I have a situation where I need a subfolder, in one domain, to redirect to a second domain, so http://domain1.com/domain2/ would redirect to http://domain2.com.

I need the root folder of domain1.com to remain as it is, just the subfolder needs to redirect.

It needs to be this way because I need to access PHP and font files on domain 2, without violating cross-domain security. Is there a way to do this using DNS?

I only found instructions on doing this using subdomain, which I believe won't work for cross-domain PHP access.

2
  • 3
    This will probably have to be some sort of .htaccess redirect. DNS doesn't do directories. It knows nothing of file systems. Sep 30, 2014 at 22:26
  • DNS isn't a protocol designed for that. What you are trying to achieve is not possible with DNS.
    – William
    Oct 1, 2014 at 12:32

3 Answers 3

4

This definitely can't be done with DNS.

I think the real question you want to solve is "How do I allow cross-domain PHP and fonts?"


To enable cross-domain PHP and font files (this is probably all you need) you would add the 'Access-Control-Allow-Origin' header to domain2's configuration, like so

Apache:

Header add Access-Control-Allow-Origin "http://domain1.com"

Nginx:

add_header Access-Control-Allow-Origin "http://domain1.com";

If you do it this way you don't need http:/domain1/domain2 at all, get rid of it as it's confusing.


If you actually want to permanently redirect domain1/subfolder > domain2 then it's a simple change in domain1's configuration like this.

Apache:

Redirect 301 /subfolder http://domain2.com

Nginx:

location /subfolder {
            return 301 http://domain2.com;
            }

Note that for both sets of answers, the Nginx directives need to be inside your server {...} blocks, and the Apache rules can go in your virtualhost config or in .htaccess (do yourself a favor and skip .htaccess if you can, put everything in your config/virtualhost for performance reasons). The Apache rules rely on mod_rewrite and mod_headers, which must be enabled.

1
  • Wow, thank you! You're right, I don't really care about the subfolders, I just didn't realize there was a simple way to allow cross-domain access like that. That's all I really needed :-) I would upvote your answer but I don't have privileges yet. Oct 1, 2014 at 14:40
1

This cannot be done via DNS. You either need to do it at the web server level (.htacess apache / rewrite rules IIS), or via a script that runs ASP.net, PHP, Perl etc. Essentially you need to change the response header to moved, and dns cannot do that.

0

You can only do this from DNS with a subdomain, not with subfolders. For subfolders, you need to do a proxy-pass - here's how it's done in Apache (version 2.4 and above):

ProxyRequests Off
ProxyPass /subfolder/ http://otherdomain.com/
ProxyPassReverse /subfolder/ http://otherdomain.com/
<Location /subfolder/>
    ProxyHTMLEnable On
    ProxyHTMLURLMap / /subfolder/
</Location>

You couls also use http://otherdomain.com:8080 if your second host serves the content on port 8080. Also, you need to have mod_proxy and mod_proxy_html enabled in your Apache server for this to work. The mod_proxy_html module is used to rewrite the links to your static resources (images, css, javascript etc.) so that if you have relative links in the response from otherdomain.com, for example, they would be loaded properly.

For older versions that don't have mod_proxy_html, you can try and use mod_substitute and some regular expressions magic to achieve the same result.

Your Answer

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

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