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

.htaccess 如何使用名称服务器将域指向外部站点上的url,.htaccess,nameservers,.htaccess,Nameservers,这就是我想要实现的目标 我有一个域A,我想更新名称服务器以使用域B(即ns1.domainb.com和ns2.domainb.com)上的自定义名称服务器设置 然后,当用户访问domainA.com时,我希望它将他们带到domainB.com/domainA 我怎样才能做到这一点?我不能通过域B上的HTACCESS文件来完成吗 当用户访问domainA.com时,我希望它能将他们带到 域名b.com/domainA 此解决方案说明了如何实现: example.com-->domainB.com/

这就是我想要实现的目标

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

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

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

当用户访问domainA.com时,我希望它能将他们带到 域名b.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请求 用户导航到,浏览器显示

在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]
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]
方法2-显式强制重定向到新位置 用户导航到,浏览器显示

在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]
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]
定制 在撰写本文时,您可以在此处测试和修改(most).htaccess规则以支持您的定制需求:


以下是实现进一步定制的提示和技巧:

RewriteRule^(.*)$http://domainB.com/domainADomina.com的.htaccess中的$1[R=301,L]
。两个域是否都指向同一个服务器位置?我不想在domainA.com上做任何事情,只想更新名称服务器。我没有域名,它实际上是一个用户网站。我正试图做到这一点,所以每当有人访问domainA.com时,都会将他们带到domainB.com上的特定页面。我已经通过转发(301重定向)实现了这一点,但我知道它也可以通过名称服务器实现。看看域名停车网站。他们要求您只需将名称服务器更新为他们的名称服务器。他们是如何做到的?将域名a.com的DNS设置为指向域名b.com服务器的IP地址。在domainB.com的根目录中,创建一个htaccess规则以重定向到/domainAHow我可以使其成为动态的,这样我就不必在我的htaccess中为每个我想这样做的域设置一行吗?如果htaccess文件规则可以是HTTP_主机,是否有办法使其正常工作!=domainb.com,如果是这样,那么重写到domainb.com/domaina?是的,事实上可以我会用你的新要求更新我的答案谢谢Drakes。我现在快凌晨1点了。让我明天试试,如果行得通,我会接受你的答案。非常感谢你的帮助!如果我遇到任何问题,我会发回。我已经重写了我的答案,使之成为一个完整的解决方案。请让我知道它是如何为您工作的重写后产生的URL应该是domainB.com/domainA.com