.htaccess redirect for parent but not for children?

I have several URLs that I need to redirect to new locations, but there are situations where child pages should remain active and not redirect. For instance:

/products will be redirected to http://www.newsite.com/products

/products/category1 will be redirected to http://www.newsite/products/category1

But /products/specialitem will not be redirected at all.

Is this possible with Redirect or RedirectMatch?

Running Redirect 301 /products http://www.newsite.com/products seems to affect all child pages

Thanks for any guidance!

Edit:

Using the answernethec answer, I was able to start. But can anyone explain why my first rule below works, while others do not?

RedirectMatch 301 ^segment-one$ http://www.google.com/

RedirectMatch 301 ^segment-one/segment-two$ http://news.google.com/

RedirectMatch 301 ^segment-one/segment-two/segment-three$ http://cnn.com/

RedirectMatch 301 ^segment-one/segment-two/segment-three/foobar$ http://gbv.com/

(Without working, I mean that I can still get to the pages, and not redirect them.)

+5
source share
3 answers

You should be able to use the following RedirectMatch rule:

 RedirectMatch 301 ^/products$ http://www.newsite.com/products 

Note that this will only redirect requests for / products, not / products /, or / products / pagename.extension.

+2
source

You can use RedirectMatch :

 RedirectMatch 301 ^/products(?!/specialitem)(.*)$ http://www.newsite.com/products$1 

This will redirect /products or anything after it other than /products/specialitem/

0
source

If you need to add conditions, this also worked for me. Notice I had to eliminate the slash between ^ and products when using the RewriteRule.

 RewriteCond [...whatever your conditions are...] # test with 302 first to avoid annoying caching issues # RewriteRule ^products$ http://www.newsite.com/products [R=302,NC,L] # but use 301 in production once you know it working RewriteRule ^products$ http://www.newsite.com/products [R=301,NC,L] 
0
source

Source: https://habr.com/ru/post/1204490/


All Articles