.htaccess-将以‘-number/’结尾的重复帖子重定向到相同的URL,但不带-number/

时间:2016-09-06 作者:andresgl

我有许多重复的帖子,我想删除它们,但谷歌已经为它们编制了索引。因此,我们的想法是将url末尾带有模式[-数字]的所有帖子重定向到没有数字的相同url

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

示例:

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/
我在上尝试了一些重写规则。htaccess,但根本不起作用。

例如,这个:

#RewriteRule ^/(.+)-[0-9]+/$  /$1 R=301
(.+) --> 它将与“职务”的字母匹配

-[0-9]+/ --> 它将匹配“-”和标题的编号

谢谢

2 个回复
SO网友:Ethan O\'Sullivan

你很接近了。将以下内容添加到.htaccess 文件位于<IfModule mod_rewrite.c> WordPress创建的标记:

RewriteCond %{HTTP_HOST}
RewriteRule ^(.+)-[0-9]+/$ /$1 [R=301]
您的.htaccess 如果未被其他插件修改,则应如下所示:

# 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
因此,它将执行以下操作:

http://example.com/category/post-title[-NUMBER]
重定向到:

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

SO网友:mmm

您可以在404模板中对此进行测试并执行重定向

尝试以下操作:

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

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


    return $template;

}, 10, 1);