How to redirect almost similar links?

Viewed 53

I need to write two redirects:

  1. From https://site.ru/dveri to https://anothersite.ru/dveri-iz-dereva/
  2. From https://site.ru/dveri?start=14 to https://anothersite.ru/blog/

I wrote two rules in htaccess:

#1

RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteRule ^ https://anothersite.ru/dveri-iz-dereva/ [L,R=301]

#2

RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteRule ^.*$ https://anothersite.ru/blog/? [L,R=301]

Result:

link https://site.ru/dveri redirects correctly to https://anothersite.ru/dveri-iz-dereva/

link https://site.ru/dveri?start=14 redirects incorrectly to https://anothersite.ru/dveri-iz-dereva/?start=14

3 Answers

The two rules you wrote are the same. Try this for the second

RewriteCond %{THE_REQUEST} /dveri\?start=14$
RewriteRule ^ https://anothersite.ru/blog/? [L,R=301]

My experienced collegue helped me with this:

RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteCond %{QUERY_STRING} ^start=14$
RewriteRule ^.*$ https://anothersite.ru/blog/? [L,R=301]

RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteRule ^ https://anothersite.ru/dveri-iz-dereva/ [L,R=301]
RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteCond %{QUERY_STRING} ^start=14$
RewriteRule ^.*$ https://anothersite.ru/blog/? [L,R=301]

RewriteCond %{THE_REQUEST} \s/+dveri[?\s/] [NC]
RewriteRule ^ https://anothersite.ru/dveri-iz-dereva/ [L,R=301]

If these directives are placed at the top of the root .htaccess file then these can be simplified to:

RewriteCond %{QUERY_STRING} ^start=14$
RewriteRule ^dveri/?$ https://anothersite.ru/blog/ [NC,QSD,R=301,L]

RewriteRule ^dveri/?$ https://anothersite.ru/dveri-iz-dereva/ [NC,R=301,L]
Related