I think this is what you are looking for
if (preg_match('@^/((?!blog|tag)[^/]+)/(?:page/)?\d+@', $_SERVER['REQUEST_URI'], $m)) {
wp_redirect("/$m[1]", 301);
}
The regexp uses lookahead to make sure that the first match isn’t blog or tag.
The '[^/]+'
part matches anything that isn’t a forward slash (/
).
(?:page/)?
– Makes an optional match against 'page/'
\d+
– Match against any number.
This will not redirect '/blog/2
if you want '/blog/2'
to be redirected but not '/blog/page/2'
then change the regexp above to read 'blog/page'
where it now says 'blog'
.
PS: I know this is 5 months old and you have probably moved on by now but I’ll put this here anyway in case someone else needs help with the same issue.