2

I am on a wordpress multisite.

I have old urls with the same subfolder name now i have to redirect old url to a new url with new subfolder name.

Example (will be simpler)

Old url 1 to new url 1:

http://site1.fr/fr-chauffage.html to https://www.site1.fr/installation_chauffage

Old url 2 to new url 2:

http://site2.fr/fr-chauffage.html to https://www.site2.fr/installateur_chauffage_dans_ma_region

So I wrote the following htaccess file

RewriteCond %{HTTP_HOST} ^site1\.fr
RewriteRule ^/?(.*)$ https://www.site1.fr/$1 [R=301,L]
RewriteRule ^fr\.html$ / [R=301,L]
RewriteRule ^fr-chauffage.html$ /installation_chauffage/ [R=301,NC,L]
 
RewriteCond %{HTTP_HOST} ^site2\.fr
RewriteRule ^/?(.*)$ https://www.site2.fr/$1 [R=301,L]
RewriteRule ^fr\.html$ / [R=301,L]
RewriteRule ^fr-chauffage.html$ /installateur_chauffage_dans_ma_region/ [R=301,NC,L]

The trouble is whatever the website the user is always redirect to site1 + good_subfolder

If I have 3 websites with the same subfolder name the user will be always redirected to site1 + good_subfolder of the new url ...

laurent
  • 67
  • 6
  • How many different domains resolve here? If you are redirecting all domains to `www` and are not using other subdomains then you don't need to explicitly list the domains (as I have done in my answer) to redirect to `www`. – MrWhite Jul 23 '20 at 00:59

1 Answers1

0

The trouble is whatever the website the user is always redirect to site1 + good_subfolder

Because the RewriteCond directive only applies to the first RewriteRule that follows. The two subsequent RewriteRule directives are executing unconditionally.

You need to repeat the condition for all rules where it needs to apply.

You're directives are also in the wrong order - you need to have the more specific rules first, otherwise you will get multiple redirects. Currently, you are redirecting everything in your first rule. And then you have a more specific redirect later.

So, are all incoming requests less the www prefix (as suggested in your examples and directives)?

Try the following instead:

# Common rules for both sites
# (Although this will result in 2 redirects if requesting the non-canonical hostname)
RewriteRule ^fr\.html$ / [R=301,L]

# Site 1 rdirects
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.fr [NC]
RewriteRule ^fr-chauffage.html$ https://www.site1.fr/installation_chauffage/ [R=301,NC,L]

# Site 2 rdirects
RewriteCond %{HTTP_HOST} ^(www\.)?site2\.fr [NC]
RewriteRule ^fr-chauffage.html$ https://www.site2.fr/installateur_chauffage_dans_ma_region/ [R=301,NC,L]

# non-www to www redirect - both sites
RewriteCond %{HTTP_HOST} ^(site1\.fr|site2\.fr) [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
MrWhite
  • 43,179
  • 8
  • 60
  • 84