简体   繁体   中英

Redirect subdomain/subfolder to root domain/subfolder - But only in SOME instances

When I set up my subdomain, I created some links incorrectly. Now Google thinks I have some pages on both my subdomain and my root domain. I need to fix this, but I can't redirect the entire subdomain.

Examples of what I want to do:

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/*

I've tried a number of .htaccess solutions and I'm close, but I can't figure it out. Here's what I've tried:

Attempt #1 - Correctly redirects, but doesn't work as a solution because it redirects everything from the subdomain

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

Attempt #2 - Doesn't redirect anything - Seems like the right solution but I'm missing something about how redirects work, I think...

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

Attempt #3 - Doesn't redirect anything

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

Attempt #4 - Doesn't redirect anything

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

My .htaccess file is in the root domain's root html folder and seems to have control. I have also tried these from the subdomain's root folder, but that didn't redirect anything.

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

%{HTTP_HOST} is the host name, mapped to domain such as sub.example.com or example.com . It does not contain any path part, that follows behind the domain. $1 is back-reference, mapped to the regex part (.*) . The RewriteRule tells if the request uri pattern starts with /move , then redirect to https://example.com/move$1 permanently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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