【问题标题】:How to point domain to url on external site using nameservers如何使用名称服务器将域指向外部站点上的 url
【发布时间】:2023-03-05 23:02:01
【问题描述】:

这是我想要完成的。

我有域 A,我想更新域名服务器以使用域 B 上的自定义域名服务器设置(即 ns1.domainb.com 和 ns2.domainb.com)。

然后,当用户访问 domainA.com 时,我希望它带他们访问 domainB.com/domainA

我怎样才能做到这一点?我不能通过 domainB 上的 HTACCESS 文件来完成这个吗?

【问题讨论】:

  • RewriteRule ^(.*)$ http://domainB.com/domainA$1 [R=301,L] 在 domianA.com 的 .htaccess 中。两个域都指向同一个服务器位置吗?
  • 我不想在 domainA.com 上做任何事情,除了更新域名服务器。我不拥有 domainA 它实际上是一个用户网站。我正在尝试获取它,因此每当有人访问 domainA.com 时,它都会将他们带到 domainB.com 上的特定页面。我已经通过转发(301 重定向)实现了这一点,但我知道它也可以通过名称服务器完成。查看域名停放网站。他们要求您将您的名称服务器更新为他们的。他们是怎么做到的?
  • 设置domainA.com的DNS指向domainB.com的服务器IP地址。在 domainB.com 的根目录下,创建一个 htaccess 规则以重定向到 /domainA

标签: .htaccess nameservers


【解决方案1】:

当用户访问 domainA.com 时,我希望它能够将他们带到 domainB.com/domainA

这个解决方案解释了如何实现:

example.com --> domainB.com/example.com

DNS 记录和 .htaccess

首先,将 domainA.com 的 DNS 设置为指向 domainB.com 的服务器的 IP 地址。在 domainB.com 的根目录中,创建一个 .htaccess 文件,其中包含将 domainA.com 请求重定向/重写到 domainB 的规则.com/domainA。有两种方法和结果。

方法一——静默重写URI请求

用户导航到http://example.com,浏览器显示http://example.com

在 domainB.com 的根目录中设置这些 .htaccess 规则:

RewriteEngine On
RewriteBase /

# Check that the request is NOT from domainB.com, e.g. domainA
RewriteCond %{HTTP_HOST} !domainB\.com$ [NC]

# Capture the top-level domainA name
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)$

# Check that we are not already requesting a valid file or directory
# This prevents inserting the subdirectory name into an already valid path
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite requests with the domainA subdirectory
RewriteRule (.*) /%1/$1 [L]

方法 2 - 显式强制重定向到新位置

用户导航到http://example.com,浏览器显示http://domainB.com/example.com

在 domainB.com 的根目录中设置这些 .htaccess 规则:

RewriteEngine On
RewriteBase /

# Check that the request is NOT from domainB.com, e.g. domainA
RewriteCond %{HTTP_HOST} !domainB\.com$ [NC]

# Capture the top-level domainA name
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)$

# Force a 301 redirect to the new URI
RewriteRule ^(.*)$ http://domainB.com/%1%{REQUEST_URI} [R=301,L]

定制

在撰写本文时,您可以在此处测试和修改(大多数).htaccess 规则以支持您的自定义要求:http://htaccess.madewithlove.be/

以下是实现进一步自定义的提示和技巧:http://mod-rewrite-cheatsheet.com/

【讨论】:

  • 我怎样才能使这个动态化,这样我就不必在我的 htaccess 中为我想要这样做的每个域添加一行?有没有办法让它在 htaccess 文件规则可能是 HTTP_HOST != domainb.com 的情况下工作,如果是这样,那么重写到 domainb.com/domaina?
  • 是的,事实上它可以。 :) 我会根据您的新要求更新我的答案
  • 谢谢德雷克斯。我现在快凌晨 1 点了。让我明天试试这个,如果它有效,我会接受你的回答。非常感谢您的帮助!如果我遇到任何问题,我会回复。
  • 我已经重写了我的答案以成为一个完整的解决方案。请让我知道你的效果如何
  • 重写后得到的URL应该是domainB.com/domainA.com
猜你喜欢
  • 2021-03-22
  • 2019-04-07
  • 2019-01-17
  • 1970-01-01
  • 2019-11-12
  • 2018-02-01
  • 1970-01-01
  • 2018-03-12
  • 2023-03-29
相关资源
最近更新 更多