【问题标题】:Redirect subdomain to another subdomain same structure subfolder将子域重定向到另一个子域相同结构的子文件夹
【发布时间】:2015-01-20 08:33:43
【问题描述】:

我要重定向子域

http://subdomain.example.com/path/to/directory/orfile.ext

结构相同但域扩展不同

http://subdomain.example.net/path/to/directory/orfile.ext

补充问题

1- 如果我有 subdomain1 sundomain 2,是否可以有一个代码来重定向站点中的所有通配符?

2- 我应该把 .htaccess 代码放在哪里?在 www 目录或每个子文件夹目录中?

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect subdomain


    【解决方案1】:

    试试这个:

    此代码写入 .htaccess 文件并放在旧域(您的域1)的根文件夹中

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
      RewriteCond %{HTTP_HOST} ^www.olddomain.com$
      RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
    </IfModule>
    

    【讨论】:

    • 它重定向主网站而不是子域
    【解决方案2】:

    试试这个

    1.复制此代码:

      Redirect 301 / http://subdomain.to-go-to.net/
    

    到一个名为.htaccess的文件中

    这将重定向所有网址,例如与他们的部分,例如

    http://subdomain.example.NET/user/login/whatever
    

    会变成

    http://subdomain.example.COM/user/login/whatever
    

    2.将.htaccess文件放到旧站点的根目录下——放到子目录下就不行了

    【讨论】:

    • 此代码仅将所有链接重定向到主链接,与我在主要问题中解释的 URL 结构不同
    【解决方案3】:

    试试这个:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(.*)example.com$ [NC]
    RewriteRule ^(.*)$  http://%1example.net/$1 [R=301,L]
    

    这会将您的example.com 域的所有请求(包括对所有子域的请求)重定向到具有相同路径结构的example.net。

    将它放在您的文档root 示例的目录中。com 站点。

    注意:

    如果您只想重定向子域(而不是主域),请改为:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(.+)\.example.com$ [NC]
    RewriteRule ^(.*)$  http://%1\.example.net/$1 [R=301,L]
    

    【讨论】:

    • 这个重定向但是当我重定向 test.example.com 它重定向到 test.example.com/test 它应该 reciredt 没有测试文件夹所以它应该是 test.example.com
    • test.example.com 应该重定向到test.example.net。请清除浏览器缓存后重新测试。
    • 不幸的是它仍然重定向到 test.example.com/test 它不应该在没有子域的 test 子文件夹的情况下重定向,子文件夹内没有其他 .htaccess 仅在主网站目录文件夹中测试此代码万维网
    • kumas 等一下,我会再次检查我的其他浏览器,我会尽快更新我的帖子
    • 我尝试过,但在所有其他浏览器、opera、firefox 上仍然相同,即,我不知道是什么原因导致重定向使子域名在 URl test.example.com/test 中重复两次.它应该是重定向 test.exacmple.com/test
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2013-04-08
    • 2012-08-13
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多