【问题标题】:Redirect subdomain/subfolder to root domain/subfolder - But only in SOME instances将子域/子文件夹重定向到根域/子文件夹 - 但仅在某些情况下
【发布时间】:2018-06-14 11:46:55
【问题描述】:

当我设置我的子域时,我错误地创建了一些链接。现在谷歌认为我的子域和根域都有一些页面。我需要解决这个问题,但我无法重定向整个子域。

我想做的例子:

https://sub.example.com/ (no redirect)
https://sub.example.com/keep-1 (no redirect)
https://sub.example.com/keep-2 (no redirect)

https://sub.example.com/move-1/* => https://example.com/move-1/*
https://sub.example.com/move-2/* => https://example.com/move-2/*

我已经尝试了许多 .htaccess 解决方案并且我很接近,但我无法弄清楚。这是我尝试过的:

尝试 #1 - 正确重定向,但不能作为解决方案,因为它会重定向子域中的所有内容

RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301,NE]

尝试 #2 - 不重定向任何内容 - 似乎是正确的解决方案,但我认为我缺少有关重定向工作原理的一些内容...

RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/ [NC]
RewriteRule ^(.*)$ https://example.com/move-1/$1 [L,R=301,NE]

尝试 #3 - 不重定向任何内容

RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/(.*)$ [NC]
RewriteRule https://example.com/move-1/$1 [L,R=301,NE]

尝试 #4 - 不重定向任何内容

RewriteBase /
RewriteRule ^sub\.example\.com/move-1/(.*)$ https://example.com/move-1/$1 [R=301]

我的 .htaccess 文件位于根域的根 html 文件夹中,并且似乎具有控制权。我也从子域的根文件夹中尝试过这些,但没有重定向任何东西。

【问题讨论】:

    标签: .htaccess redirect mod-rewrite subdomain


    【解决方案1】:
    RewriteCond %{HTTP_HOST} ^sub\.example\.com$
    RewriteRule ^move(.*) https://example.com/move$1 [R=301,L]
    

    %{HTTP_HOST} 是主机名,映射到域,例如sub.example.com 或example.com。它不包含域后面的任何path 部分。 $1 是反向引用,映射到正则表达式部分 (.*)。 RewriteRule 告诉请求 uri 模式是否以 /move 开头,然后永久重定向到 https://example.com/move$1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      相关资源
      最近更新 更多