大网数据广告活动
网站运营与故障问答

How to use DNS to set a subfolder of one domain to redirect to another domain

· 52CE 我爱测

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 …

问题原文

原帖链接 · James Morrison · CC BY-SA 4.0

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.

已采纳解答

原帖链接 · Tom Brossman · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2014-09-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。