Redirect “any page/page/number/” to “any page”

I’ve read so many similar topics but none of them matches my own request.

I’d like to redirect in ht.access any page with pagination to the root-main-original page.

example.com/garden/page/2
redirected to example.com/garden/

example.com/house/bedroom/page/14
redirected to example.com/house/bedroom/

example.com/cat/subcat1/subcat2/subcat3/subcat4/page/3/
redirected to example.com/cat/subcat1/subcat2/subcat3/subcat4/

Can you help?

1 Answer
1

I’d like to redirect in ht.access

To clarify, it’s .htaccess, not ht.access.

You can do something like the following at the top of the .htaccess file, before the existing WordPress directvies:

RewriteRule (.*/)page/\d+/?$ /$1 [R=302,L]

The above issues a 302 (temporary) redirect for any URL of the form /<something>/page/<number>/ (trailing slash optional) to /<something>/.

The $1 backreference contains the captured URL-path before the page/<number>/ part at the end of the URL-path.

Always test with a 302, even if this should be a 301 (permanent) redirect later.

There is no need to repeat the RewriteEngine directive, since that should already appear later in the file.

Leave a Comment