简体   繁体   中英

Redirecting 301 from SubDomain to SubFolder / with diffrent URLs using htaccess

i have a web with this address(subdomain): subd.example.com and want to redirect it to (subfolder): https://www.example.com/subf/

is I searched the code bellow works if the urls in my websites wont change:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subd\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/subf/$1/ [R=301]

but some or most of my URLs will be changed for example from /oldlink1 to /newlink1 and ...

somehow im stuck and the codes wont work, only home page redirects correctly, i tried:

Redirect 301 /oldlink1 www.example.com/subf/newlink1

but it doesnt work, returns me this link: www.example.com/subf/oldlink

any help please?

EDIT: if it is important both subdomain and subfolder names are the same, I mean subd and subf are both sub . addresses are:

sub.example.com
www.example.com/sub/

Your issue most likely is the position of those specific redirection rules. This is important, you need to consider that the rule set is processed top to bottom. So you need to implement specific exceptions first, so further up, and more general rules later, so further down.

This probably is what you are looking for:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?$ https://example.com/subf [R=301,L]

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?oldlink1/?$ https://example.com/subf/newlink1 [R=301,L]

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?oldlink2/?$ https://example.com/subf/newlink2 [R=301,L]

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?oldlink3/?$ https://example.com/subf/newlink3 [R=301,L]

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?(.*)/?$ https://example.com/subf/$1 [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