Home > Back-end >  Regex rule for .htaccess
Regex rule for .htaccess

Time:05-31

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.

CodePudding user response:

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/

  • Related