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

时间:2015-03-30 02:43:40

标签: .htaccess nameservers

这是我想要完成的事情。

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

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

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

1 个答案:

答案 0 :(得分: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 。有两种方法和结果。

方法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/

相关问题