将 subdomain/subfolder 重定向到根 domain/subfolder - 但仅在某些情况下
Redirect subdomain/subfolder to root domain/subfolder - But only in SOME instances
当我设置我的子域时,我错误地创建了一些链接。现在 Google 认为我的子域和根域上都有一些页面。我需要解决这个问题,但我无法重定向整个子域。
我想做的事情的例子:
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/ [L,R=301,NE]
尝试 #2 - 不重定向任何东西 - 似乎是正确的解决方案,但我想我遗漏了一些关于重定向如何工作的信息......
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/ [NC]
RewriteRule ^(.*)$ https://example.com/move-1/ [L,R=301,NE]
尝试 #3 - 不重定向任何内容
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/(.*)$ [NC]
RewriteRule https://example.com/move-1/ [L,R=301,NE]
尝试 #4 - 不重定向任何内容
RewriteBase /
RewriteRule ^sub\.example\.com/move-1/(.*)$ https://example.com/move-1/ [R=301]
我的 .htaccess 文件在根域的根 html 文件夹中,似乎有控制权。我也从子域的根文件夹中尝试过这些,但没有重定向任何东西。
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule ^move(.*) https://example.com/move [R=301,L]
%{HTTP_HOST}
是主机名,映射到域,例如 sub.example.com
或 example.com
。它不包含域后面的任何 path
部分。 </code> 是反向引用,映射到正则表达式部分 <code>(.*)
。 RewriteRule
告诉请求 uri 模式是否以 /move
开头,然后永久重定向到 https://example.com/move
。
当我设置我的子域时,我错误地创建了一些链接。现在 Google 认为我的子域和根域上都有一些页面。我需要解决这个问题,但我无法重定向整个子域。
我想做的事情的例子:
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/ [L,R=301,NE]
尝试 #2 - 不重定向任何东西 - 似乎是正确的解决方案,但我想我遗漏了一些关于重定向如何工作的信息......
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/ [NC]
RewriteRule ^(.*)$ https://example.com/move-1/ [L,R=301,NE]
尝试 #3 - 不重定向任何内容
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/(.*)$ [NC]
RewriteRule https://example.com/move-1/ [L,R=301,NE]
尝试 #4 - 不重定向任何内容
RewriteBase /
RewriteRule ^sub\.example\.com/move-1/(.*)$ https://example.com/move-1/ [R=301]
我的 .htaccess 文件在根域的根 html 文件夹中,似乎有控制权。我也从子域的根文件夹中尝试过这些,但没有重定向任何东西。
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule ^move(.*) https://example.com/move [R=301,L]
%{HTTP_HOST}
是主机名,映射到域,例如 sub.example.com
或 example.com
。它不包含域后面的任何 path
部分。 </code> 是反向引用,映射到正则表达式部分 <code>(.*)
。 RewriteRule
告诉请求 uri 模式是否以 /move
开头,然后永久重定向到 https://example.com/move
。