2

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

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
3
  • HI Ethan Jinks O'Sullivan Thanks a lot for your answer. I completely forgot about this question. In the end, I removed the duplicated post and I asked Google to reindex the site.. After few months every is OK with the SEO. If I have in the future similar problem I will try your code. Thanks again!
    – andresgl
    Dec 14, 2016 at 11:53
  • @andresgl Glad you solved your issue. Please click the checkmark next to my answer to indicate that this question has been solved. Dec 14, 2016 at 17:38
  • No longer works here, end in a server error
    – Rich
    Mar 14, 2018 at 9:15
0

you can test this in the 404 template and do a redirection

try this :

add_filter("404_template", function ($template) {

    var_dump($GLOBALS["wp_query"]->query);


    return $template;

}, 10, 1);
5
  • Hi @mmm not sure at all what you mean.. Thank you!
    – andresgl
    Sep 6, 2016 at 12:42
  • you can do redirection in this action instead of the .hatccess file
    – mmm
    Sep 6, 2016 at 12:59
  • Could you give an example, please? I am not sure how to do it. Thanks
    – andresgl
    Sep 8, 2016 at 10:20
  • have you try the code of my answer ?
    – mmm
    Sep 8, 2016 at 10:47
  • Yes.. any change at all www.domain.com/category/post-title-3/ still www.domain.com/category/post-title-3/ and not to www.domain.com/category/post-title Thanks
    – andresgl
    Sep 8, 2016 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.