Change order of your rules like this:
RewriteRule ^page/sub-page/(.+)$ /$1 [R=301,L]
RewriteRule ^page/(.+)$ /$1 [R=301,L]
EDIT: Or combine it a single rule:
RewriteRule ^page/(?:.+?/)?([^/]+)$ /$1 [R=301,L]
Question
I have some problem with RewriteRule
. I'm moving old shop to new one and path to some products will be changed. The old path to one single product is:
www.example.com/page/product1.html
www.example.com/page/sub-page/product1.html
Every single product (in new shop) can be found on that path:
www.example.com/product1.html
And it is the best and (in my opinion) the easiest way to redirect all the products. How to cut the part of previous links? My rule looks like this:
RewriteRule ^page/(.*)$ /$1 [R=301,L]
RewriteRule ^page/sub-page/(.*)$ /$1 [R=301,L]
but every time when I enter to the page using old path www.example.com/page/sub-page/product1.html
the link after redirect looks like that: www.example.com/sub-page/product1.html
and should www.example.com/product1.html
Solution
Change order of your rules like this:
RewriteRule ^page/sub-page/(.+)$ /$1 [R=301,L]
RewriteRule ^page/(.+)$ /$1 [R=301,L]
EDIT: Or combine it a single rule:
RewriteRule ^page/(?:.+?/)?([^/]+)$ /$1 [R=301,L]
OTHER TIPS
You have to switch positions of your rules like this:
RewriteRule ^page/sub-page/(.*)$ /$1 [R=301,L]
RewriteRule ^page/(.*)$ /$1 [R=301,L]
The rewrite module just goes down your list of rules and tries to apply the rule one by one. Your first rule would match both.