繁体   English   中英

将子域/子文件夹重定向到根域/子文件夹-但仅在某些实例中

[英]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/$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文件夹中,并且似乎可以控制。 我也从子域的根文件夹尝试了这些,但是没有重定向任何东西。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM