简体   繁体   中英

.htaccess custom URL forwarding rules using pattern matching

I have been attempting (without success) to implement the following logic in .htaccess:

www.domain.com/ / redirects to www .domain.com/ / /重定向到www .domain.com / /
www.domain.com/ / redirects to www .domain.com/ / /重定向到www .domain.com / /
www.domain.com/ / redirects to www .domain.com/ / /重定向到www .domain.com / /
www.domain.com/ / redirects to www .domain.com/ / /重定向到www .domain.com / /
www.domain.com/ / redirects to www .domain.com/ / /重定向到www .domain.com / /

.htaccess rules also needs to preserve all directories/page names in the URL:

www.domain.com/ /collectionA/productB / collectionA /产品B
redirects to
www .domain.com/ /collectionA/productB .domain.com / / collectionA / productB

www.domain.com/ /collectionC/productD / collectionC / productD
redirects to
www .domain.com/ /collectionC/productD .domain.com / / collectionC / productD

www.domain.com/ /collectionD/productE / collectionD / productE
redirects to
www .domain.com/ /collectionD/productE .domain.com / / collectionD / productE


Any solutions would be most welcome! Thank you.

Place this code in your DOCUMENT_ROOT/.htaccess file of domain.com :

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^aa(/.*)?$ http://www2.domain.com%{REQUEST_URI} [NC,NE,R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^bb(/.*)?$ http://www3.domain.com%{REQUEST_URI} [NC,NE,R=301,L]

Place this code in your /cc/.htaccess , /dd/.htaccess and /ee/.htaccess :

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://www4.domain.com%{REQUEST_URI} [NC,NE,R=301,L]

Update: As per comments you can have this rule in root .htaccessof each domain:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www2\.domain\.com$ [NC]
RewriteRule ^/?$ /aa/ [R=301,L]

RewriteCond %{HTTP_HOST} ^www3\.domain\.com$ [NC]
RewriteRule ^/?$ /ab/ [R=301,L]

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