0

Who can help me with regex rule?

If visited page is https://example.com/shop/product then I need to redirect user to https://example.com/product, but if visited page is https://example.com/shop/page/2 I need to do nothing.

There is possibility, that user landed on https://example.com/shop/category/child-category/product and for this example I need to redirect to https://example.com/category/child-category/product

I have already wrote this rule: RewriteRule ^shop/(.*)$ /$1 [R=301,L], but this rule redirects user from this page if he is in second or other pages.

2
  • ok but then how do you define for which URLs you want redirects and for which you don't? Commented May 31, 2022 at 9:22
  • If there is /shop/page/2 then I don't need to redirect, but if user landed on page which is /shop/category/product, then I need to redirect to /category/product. So rule need to check if there is keyword page then do nothing, but if there is then do redirect. Commented May 31, 2022 at 10:00

1 Answer 1

3

You can use a negative lookahead condition like this:

RewriteRule ^shop/(?!page/)(.+)$ /$1 [R=301,L,NC]

(?!page/) is a negative lookahead condition that will fail the match if page/ appears after shop/

Sign up to request clarification or add additional context in comments.

2 Comments

It works, but I have forgot to mention that there ar possibility, that user landed on https://example.com/shop/category/child-category/product and for this example your rule doesn't redirect to https://example.com/category/child-category/product
ok try my updated rule now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.