当用户访问 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/