如何使用名称服务器将域指向外部网站上的url



以下是我想要实现的目标。

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

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

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

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

此解决方案解释了如何实现:

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

DNS记录和.htaccess

首先,将domainA.com的DNS设置为指向domainB.comomainB.com的根目录中,创建一个.htaccess文件,该文件具有将domainA.comB.com/domainA中的规则。有两种方法和结果。

方法1-静默重写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/

最新更新