.htaccess – Redirect duplicated post ended in ‘-number/’ to the same url without the -number/

I have many duplicated post and I wanted remove them but Google already indexed them. So the idea is redirect all post with pattern [-number] in the end of the url to the same URL without number

www.domain.com/category/post-title[-number] to www.domain.com/category/post-title

Example:

www.domain.com/category/post-title/

www.domain.com/category/post-title-1/ -->  www.domain.com/category/post-title/

www.domain.com/category/post-title-2/ -->  www.domain.com/category/post-title/

www.domain.com/category/post-title-3/ -->  www.domain.com/category/post-title/

www.domain.com/category/post-title-4/ -->  www.domain.com/category/post-title/

www.domain.com/category/post-title-5/ -->  www.domain.com/category/post-title/

www.domain.com/category/post-title-6/ -->  www.domain.com/category/post-title/

I have tried some Rewrite Rules on the .htaccess but didn’t work at all.

For example this one:

#RewriteRule ^/(.+)-[0-9]+/$  /$1 R=301

(.+) –> it will match the letters of the ‘post-title’

-[0-9]+/ –> It will match the ‘-‘ and the number of the tittle

Thanks!

2 Answers
2

You’re close. Add the following to your .htaccess file in between the <IfModule mod_rewrite.c> tags that were created by WordPress:

RewriteCond %{HTTP_HOST}
RewriteRule ^(.+)-[0-9]+/$ /$1 [R=301]

Your .htaccess should looks like the following if it hasn’t been modified by another plugin:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# Custom Rewrite
RewriteCond %{HTTP_HOST}
RewriteRule ^(.+)-[0-9]+/$ /$1 [R=301]
</IfModule>
# END WordPress

As a result it will do the following:

http://example.com/category/post-title[-NUMBER]

Redirects to:

http://example.com/category/post-title

Leave a Comment