首页 > 解决方案 > 如何使用 htaccess 将子域(包含所有文件夹和文件)重定向到另一个域?

问题描述

我想将 subdomain.example.com 和子域访问的所有可能的 URL(如 subdomain.example.com/somefolder/somefile.php)重定向到另一个域 example-two.com。htaccess可以吗?

标签: .htaccesssubdomainaddon-domain

解决方案


对于将 subdomain.example.com/any-address 重定向到 example-two.com 将此规则添加到您的 subdomain.example.com .htaccess 文件的顶部

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.example\.com [NC]
    RewriteRule (.*) http://example-two.com/ [R=301,L]
</IfModule>

对于将 subdomain.example.com/any-address 重定向到 example-two.com/any-address 将此规则添加到您的 subdomain.example.com .htaccess 文件的顶部

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.example\.com [NC]
    RewriteRule (.*) http://example-two.com/$1 [R=301,L]
</IfModule>

推荐阅读