简体   繁体   中英

How to redirect non-www and www domain with specific sub folder to root?

I have 3 domain names:

www.domain1.com
www.domain2.com
www.domain3.com

They all share the same sub folder URL:

www.domain1.com/sub1/sub2
www.domain2.com/sub1/sub2
www.domain3.com/sub1/sub2

I need to redirect everything in sub2 and below it to the site's homepage:

www.domain1.com/sub1/sub2  --> www.domain1.com
domain1.com/sub1/sub2 --> www.domain1.com


www.domain2.com/sub1/sub2  --> www.domain2.com
domain2.com/sub1/sub2 --> www.domain2.com


www.domain3.com/sub1/sub2  --> www.domain3.com
domain3.com/sub1/sub2 --> www.domain3.com

Here's what I have so far:

RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^domain3\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain3\.com$ [NC]
RewriteRule ^sub1/sub2(.*)$ / [NC,L,R=301]

Problem:
It's only working for www.domain1.com, www.domain2.com, www.domain3.com .

It does not work for URLs without the "www". So does not work: domain1.com/sub1/sub2

Thanks!

You may try this:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2|domain3)\.com
RewriteCond %{REQUEST_URI}  ^/sub1/sub2/?  [NC]
RewriteRule .*   http://%{HTTP_HOST}/     [R=301,L]

Redirects any URL with this scheme

http://www.domain1.com/sub1/sub2 or http://domain1.com/sub1/sub2

To corresponding root, removing the segment /sub1/sub2 .

I assumed /sub1/sub2 are fixed strings. If they aren't, replace

RewriteCond %{REQUEST_URI}  ^/sub1/sub2/?  [NC]

with

RewriteCond %{REQUEST_URI}  ^/[^/]+/[^/]+/?  [NC]

The URL scheme with 2 folders in the path /sub1/sub2/ must be kept for the rule to work.

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